0

I am looking forward to store each line a validate a specific condition while parsing the XML file using SAX parser in Java.

Issue: when I convert the character [] to a new string with Characters(...) method of the parser, I am not getting the line text properly.

Expected: Need to store a line of XML file inside a String variable.

Code:

public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
if (text) {
String str = new String(arg0,arg1,arg2);
System.out.println(str.trim());
}}
4

3 回答 3

1

人们在使用 SAX 时犯的第一个错误是他们没有意识到 XML 解析器可以随意分解文本节点并在 characters() 回调上进行多次调用。您需要累积这些值直到文本节点结束,由下一个非字符事件标记,例如调用 startElement 或 endElement。

您谈到文本的“行”,但不要期望 XML 解析器一次将文本传递到一行。(一些解析器可能:规范允许但不需要它。)

于 2013-11-13T08:15:29.127 回答
0

尝试这个!使用 aLinkedList将标签名称存储在 every并使用at everystartElement()删除最后一个元素。用于从中获取数据。因此,每次函数返回一些实际数据(使用)时,您都可以将其与最后一个元素()相关联pollLast()endElement()String.trim()characters()characters()String.length()!=0peekLast()LinkedList

然后你可以选择append()

于 2014-01-28T12:10:35.643 回答
0

你需要一个ignorableWhitespace()回调。

但你的目标是有问题的。XML 不由行组成,它由元素和属性组成。

于 2013-11-13T05:12:06.623 回答