1

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.

4
  • 2
    Can you provide code that calls the parser and the corresponding XML document? Commented Dec 17, 2014 at 8:52
  • Is it fair to say your overall question is "How do I get the value of the element <xsd:name>?"? Commented Dec 17, 2014 at 9:02
  • No i don't think so. I get values on other xmls. So i think this is some bug in java sax parser Commented Dec 17, 2014 at 9:07
  • 1
    You really want to believe that you happen to find a bug in a very common API part of technology used by thousands of programmers every day that is over a decade old? Blaming the tools gets you absolutely nowhere, it is not in your interest to want such things to be true. Blame yourself until you really have 100% proof that it is a bug, as long as it is a you-problem you can fix it. Commented Dec 17, 2014 at 11:00

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.