I am parsing xml using the SAXParser and want to know if this is the right way to implement the characters method.
Assume there’s a class-level String variable named elementValue and it is initialized to “” in the startElement method.
Here is the characters method:
@Override
public void characters(char[] ch, int start, int length)
{
String charsToAppend = new String(ch, start, length);
elementValue = elementValue + charsToAppend;
}
Is it correct that we need to append to the value we have so far and is the append being done correctly?
Also, why does the characters method give the start index? Won’t it be zero every time?
Thanks.
Yes, you need to append because there is no guarantee that the chunk of data read will contain all of the element text.
The start position is given and needs to be used because the other characters in the array can be junk.
However, you should use a
StringBufferinstead, and call its’append()method, like so: