SAX parser: problems with overwritten characters method

L

leo

I have created a sax parser to parse elements from a XML file into a
database. I thought everything worked smooth, but while testing I
found that sometimes element values were incorrectly parsed. Sometimes
just the last part of the value is parsed (e.g. "00" instead of
"2620200000000") I can't specify when exactly it happens, but it
always happens at the same place in the same XML file.
Using the debugger I found what happens, however I don't know a proper
way to solve it.

This is the element:
<IMSI>262020000000000</IMSI>
The method 'public void characters(char ch[], int start, int length)'
assigns the element value to an element. The char array ch has a
length of 2048 chars. When parsing the value "262020000000000" in this
case it uses 2 passes, because it appearantly didn't fit in the buffer
completely.
In the first step start=2035 and length = 13; this delivers
"26202000000"
At the second step the buffer has been refreshed, length = 2, start =
0. This returns "00". Resulting in the final value"00" for IMSI which
is not correct. Do I have to work around this with adding strings or
is anything else I can do here?

kind regards,

leo
 
A

Adam Maass

leo said:
This is the element:
<IMSI>262020000000000</IMSI>
The method 'public void characters(char ch[], int start, int length)'
assigns the element value to an element. The char array ch has a
length of 2048 chars. When parsing the value "262020000000000" in this
case it uses 2 passes, because it appearantly didn't fit in the buffer
completely.
In the first step start=2035 and length = 13; this delivers
"26202000000"
At the second step the buffer has been refreshed, length = 2, start =
0. This returns "00". Resulting in the final value"00" for IMSI which
is not correct. Do I have to work around this with adding strings or
is anything else I can do here?

You should always expect this behavior in the characters() method -- it's
documented this way. You need to use a StringBuffer and append the values
you get, even though in the vast majority of cases, it'll only use one pass.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top