This is weird..
I’ve a DefaultHandler that is actually handling everything well but sometimes, for no apparent reasons, it will give me 0 instead of the correct value. This is happening everytime in the same place so there is something wrong but I cannot figure out what!
The problem is this. I’m parsing a time like “17:00:00”. When the parser arrive at the “strange time” it will parse it like this -> “17:00:0”, call one more time the method characters(char[] ch, int start, int end), and parse the last “0”.
WTF?
Pratically it will set the end to 7 instead of 8. And this is working for everything except for a few values!
The code is pretty straightforward, it’s just a simple saxparser. This is the characters method:
public void characters(char[] ch, int start, int end) {
if(on_element)
value = new String(ch, start, end);
}
Any hint?
The
charactersmethod can be called more than once for the text within a single pair of open and close tags.Your code assumes it’s only called once, which will frequently be true for small data, but not always.
You need to initialize a buffer in the
startElementmethod for that tag, collect into the buffer in thecharactersmethod, and convert the buffer to a string in theendElement.My answer to this question where the user was making the same mistake contains some code doing this to correct more complete code that user had posted. Reading that may help you with your code.