【发布时间】:2012-04-14 04:51:32
【问题描述】:
知道我在这里做错了什么。
这是xml文件
<text xml:space="preserve">{{Redirect|Anarchist|the fictional character|Anarchist (comics)}}
{{Redirect|Anarchists}}
{{Anarchism sidebar}}
{{Libertarianism sidebar}}
</text>
现在当我在 SAX 解析器的帮助下解析它时,例如这是我的字符方法
public void characters (char ch[], int start, int length) throws SAXException{
System.out.println(text);
if (text){
System.out.println(testData); //testData is StringBuilder
if (testData != null){
for (int j=start; j < (start + length); j++){
testData.append(ch[j]);
}
}
}
text = false
}
这是我的 startElement 方法
public void startElement(String uri, String localname, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("text")) {
text = true;
}
}
但我的字符函数只被调用一次。我以为它会被调用几次,然后我可以附加
【问题讨论】:
-
鉴于您提供的代码,我很惊讶
characters函数被调用一次。我没有看到任何调用characters函数的代码... -
您的代码甚至无法编译。请修复它,然后编辑您的问题并输入正确的代码。此外,格式使代码流非常混乱,我怀疑这是您的问题的原因。
-
各位,我刚才提到了 SAX Parser 的两种方法。其他方法是 startElement 和 endElement。当我们在 startElement 方法中将标志文本设置为 true 时,就会调用 characters 方法。代码编译正常。只是它读取,{{Redirect|Anarchist|虚构人物|Anarchist (comics)}} {{Redirect|Anarchists}} 的 XML 文件,而不是整个文本