public void characters(char[] ch, int start, int length)
throws SAXException {
for (int i=0; i<ch.length; i++)
System.out.print(i);
}
The mistake here is in reading from the beginning to the end of the
character array. This natural "gotcha" results from years
of iterating through arrays, either in Java, C, or another language.
However, in the case of a SAX event, this can cause quite a bug. SAX
parsers are required to pass starting and length values on the
character array that any loop constructs should use to read from the
array. This allows lower-level manipulation of textual data to occur
in order to optimize parser performance, such as reading data ahead
of the current location as well as array reuse. This is all legal
behavior within SAX, as the expectation is that a wrapping
application will not try to "read past" the length
parameter sent to the callback.
Mistakes as in the example shown can result in gibberish data being
output to the screen or used within the wrapping application, and are
almost always problematic for applications. The loop construct looks
very normal and compiles without a hitch, so this gotcha can be a
very tricky problem to track down. Instead, you can simply convert
this data to a String, use it, and never worry:
public void characters(char[] ch, int start, int length)
throws SAXException {
String data = new String(ch, start, length);
// Use the string
}