JAVA SAX parser split calls to characters()

Solution 1:

Parser is calling characters method more than one time, because it can and allowed per spec. This helps fast parser and keep their memory footprint low. If you want a single string create a new StringBuilder object in the startElement and process it on endElement method.

Solution 2:

You will be surprised but this is a documented behavior i.e. you can't assume that the parser will read and return all the text-data of an element in a single callback. I had the same experience earlier. You need to code to handle this situation or you can switch to Stax parser. You can use CharArrayWriter to accumulate the data across multiple callbacks.

See below from the JavaDoc of ContentHandler.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.

Solution 3:

You can change start, end and character method like:

  • add a "global" content variable
  • then null it in start method (content == null)
  • in end method u can println or add that content string to some object
  • in character method u can make if/else:

    if (content == null)
    {
        content = new String(ch, start, length);
    } else {
        content += new String(ch, start, length);
    }
    

    Brutal way (better to do it with stringbuilder) but works and "string" is not longer splitted.

Solution 4:

This is a feature of SAX. The parser can split the Text segments and call your characters method as many times as it likes.

The reason for this is performance, which SAX prioritises over ease of use. SAX may have used up its internal buffer so to avoid copying it passes the data it has so far through to your code.