Java XMLReader不清除多字节UTF-8编码属性

mck*_*mey 5 java sax xmlreader utf-8 character-encoding

我有一个非常奇怪的情况,我的SAX ContentHandler被XMLReader传递给错误的属性.正在解析的文档是UTF-8,XML属性中包含多字节字符.似乎发生的是每次调用我的处理程序时都会累积这些属性.因此,它们不是连续传递,而是连接到前一个节点的值.

以下是使用公共数据(维基百科)演示此示例的示例.

public class MyContentHandler extends org.xml.sax.helpers.DefaultHandler {

    public static void main(String[] args) {
        try {
            org.xml.sax.XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
            reader.setContentHandler(new MyContentHandler());
            reader.parse("http://en.wikipedia.org/w/api.php?format=xml&action=query&list=allpages&apfilterredir=redirects&apdir=descending");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) {
        if ("p".equals(qName)) {
            String title = attributes.getValue("title");
            System.out.println(title);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:这个完整的例子产生(对于粗俗的输出向任何广东话者道歉):











Run Code Online (Sandbox Code Playgroud)

有没有人知道发生了什么以及如何解决它?文档中的内容与我通过此代码段进行调试时发生的情况不符.

mck*_*mey 1

似乎是包含 JRE 的 Xerces 版本中的一个错误 ( com.sun.org.apache.xerces.internal.parsers.SAXParser)。下面是我的笔记。

与 JRE 1.6.0_24v2.4.0v2.5.0v2.6.0 捆绑的版本确实会进行属性累积。

Xerces-J v1.4.4 似乎没有该错误。

Xerces2-J v2.6.1v2.6.2v2.9.02.11.0 似乎没有该错误。

您可以通过测试的版本看出我将版本历史记录一分为二。似乎是v2.6.0 和 v2.6.1 之间修复的问题。我有点惊讶 JRE 没有更新,因为大约 7 年前它就在主 Xerces 中修复了!