I have a string input from which I need to extract simple information, here is the sample xml (from mkyong):
&l
I'm going to highlight another issue, which you're likely to hit once you read your file correctly.
The method
public void characters(char ch[], int start, int length)
won't always give you the complete text element. It's at liberty to give you the text element (content) 'n' characters at a time. From the doc:
SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks
So you should build up your text element string from each call to this method (e.g. using a StringBuilder
) and only interpret/store that text once the corresponding endElement()
method is called.
This may not impact you now. But it'll arise at some time in the future - likely when you least expect it. I've encountered it when moving from small to large XML documents, where buffering has been able to hold the whole small document, but not the larger one.
An example (in pseudo-code):
public void startElement() {
builder.clear();
}
public void characters(char ch[], int start, int length) {
builder.append(new String(ch, start, length));
}
public void endElement() {
// no do something with the collated text
builder.toString();
}