0

I am using saxparser in order to get xml tag contents from file. All works properly for a non long data xml. However, when arribed at certain position, characters() event received truncated ch[] and is triggered twice. For example for 1234567890 is received twice and value is truncated like following example. How to clear char array or memory?

id  1234567890
name myName1
id  1234567890
name myName2
...
(error)
id 1234567
id 890
name myName3

Code:

private StringBuffer buf = new StringBuffer(2048);

            @Override
            public void characters(char ch[], int start, int length) throws SAXException {  

            if(this.v_Id){

                buf.append(ch, start, length);

                myParsedXMLDataSet.setId(buf.toString());

                Log.d("id", buf.toString());

                buf.delete(0, buf.length());

            }
Bo.
  • 2,547
  • 3
  • 24
  • 36
Jaume
  • 3,672
  • 19
  • 60
  • 119

1 Answers1

3

characters() is allowed to be called multiple time for the same element (see documentation). You should not assume that it will only be called once, even though most time that is the case. Append to the buffer each time characters() is called, then store the value when endElement() is called.

unholysampler
  • 17,141
  • 7
  • 47
  • 64