简体   繁体   中英

Java sax parser bug

I am using java sax parser and i override

@Override
public void characters(char ch[], int start, int length) throws SAXException {
    value = new String(ch, start, length);

in some case array ch contains qName of element but not contains entire value.

Example:

ch = [... , x, s, d, :, n, a, m, e, >, 1, 2, 3]

but the real value of xsd:name is 123456789

EDIT

String responseString = Utils.getXml(url);

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
handler = new SimpleHandler();
saxParser.parse(new InputSource(new StringReader(responseString)), handler);

List<Entit> list = handler.getList();

I have xml like this (ofcourse the original xml is much bigger)

<root>
   <el>
     <xsd:name>11111111</xsd:name>
   </el>
   <el>
     <xsd:name>22222222</xsd:name>
   </el>
   <el>
     <xsd:name>123456789</xsd:name>
   </el>
   <el>
     <xsd:name>333333333</xsd:name>
   </el>
</root>

i get error just for one value in xml.

How to fix that.

The characters method does not necessarily return the entire set of characters. You need to store the result each time characters is called, something like:

final StringBuilder sb = new StringBuilder();

@Override
public void characters(char ch[], int start, int length) throws SAXException {
    sb.append(ch, start, length);
}

You then need to reset your StringBuilder (or whatever you are using) when you find an end element tag or a begin element tag or whatever the case may be.

Read the specification for characters :

"The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information."

Generally, what you should do is delete the text buffer when you see startElement or endElement . Usually you will do something with the current buffer when these are seen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM