字符方法不捕获元素的所有数据 - SAX解析器 - Java

时间:2022-10-29 13:56:40

I am parsing an XML document using the SAX parser. I am using the characters method to capture the data provided between two element tags, accounting for the fact that the data will be provided in chunks :

我正在使用SAX解析器解析XML文档。我使用characters方法捕获两个元素标签之间提供的数据,考虑到数据将以块的形式提供:

StringBuilder currentText = new StringBuilder();

...

public void characters(char ch[], int start, int length) {
        if (currentText!=null) {
            for (int i=start; i<start+length; i++) {
                currentText.append(ch[i]);
            }

Then in the endElement method I am using :

然后在我使用的endElement方法中:

public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        System.out.println("Current Text is " + currentText.toString());
        currentText.setLength(0);

}

The problem is that when I look in the log, currentText is not capturing the entire contents of some of the larger data fields in the XML.

问题是,当我查看日志时,currentText不会捕获XML中某些较大数据字段的全部内容。

Does anyone know why this could be happening ?

有谁知道为什么会发生这种情况?

Thank you.

1 个解决方案

#1


0  

Well, there's always the possibility that currentText==null - you can eliminate that possibility, we can't.

好吧,总是有可能currentText == null - 你可以消除这种可能性,我们不能。

Note that your character loop to do the append is horribly inefficient: you can use

请注意,执行追加的字符循环非常低效:您可以使用

public StringBuilder append(char str[], int offset, int len)

public StringBuilder append(char str [],int offset,int len)

You might be missing some whitespace reported to the ignorableWhitespace callback, but I guess if it was only whitespace missing, you would have told us.

你可能会遗漏一些报告给ignorableWhitespace回调的空格,但我想如果它只是空白缺失,你会告诉我们的。

Sorry, can't suggest any more. Follow my grandmother's motto: if you're looking for something and can't find it, that means you are looking in the wrong place; so look somewhere else.

对不起,不能再提示了。按照我祖母的座右铭:如果你正在找东西而找不到它,那就意味着你正在寻找错误的地方;所以看看其他地方。

#1


0  

Well, there's always the possibility that currentText==null - you can eliminate that possibility, we can't.

好吧,总是有可能currentText == null - 你可以消除这种可能性,我们不能。

Note that your character loop to do the append is horribly inefficient: you can use

请注意,执行追加的字符循环非常低效:您可以使用

public StringBuilder append(char str[], int offset, int len)

public StringBuilder append(char str [],int offset,int len)

You might be missing some whitespace reported to the ignorableWhitespace callback, but I guess if it was only whitespace missing, you would have told us.

你可能会遗漏一些报告给ignorableWhitespace回调的空格,但我想如果它只是空白缺失,你会告诉我们的。

Sorry, can't suggest any more. Follow my grandmother's motto: if you're looking for something and can't find it, that means you are looking in the wrong place; so look somewhere else.

对不起,不能再提示了。按照我祖母的座右铭:如果你正在找东西而找不到它,那就意味着你正在寻找错误的地方;所以看看其他地方。