0

I found some posts related to this issue but not able to find the solution. Problem is that on character() function, ch[] array contains incomplete or last elements from last call and this happens sometimes but 98% of times works properly. File name has no special chars and no strange names.

if we have,

<tag>image1test.jpg<tag> 
<tag>image2bla.jpg<tag>

when working, char array contains proper values but when not, when characters function is called for second tag from example, we get,

[i,m,a,g,e,2,b,e,s,t] (residual chars from last call)

how to solve it? thanks.

@Override
public void characters(char ch[], int start, int length) {
if(this.v_new){
myNewsXMLDataSet.setNews(new String(ch, start, length));

}
Jaume
  • 3,672
  • 19
  • 60
  • 119

1 Answers1

1

The parser isn't required to call characters(char[],int,int) on your handler only once. You've got to expect multiple calls, and process the value only after the endElement() method has been called.

public class Handler extends DefaultHandler {

    private StringBuilder sb = new StringBuilder();
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        sb.append(ch, start, length);

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        doStuffWith(sb.toString());
        sb = new StringBuilder();
    }
}
David Bullock
  • 6,112
  • 3
  • 33
  • 43
  • I don't know if there is a StringBuilder in the Android environment. My sample is from vanilla Java. You might not choose to do exactly that, but it illustrates how the parser can call `characters()` whenever it likes, and you've got to be ready for it. – David Bullock Dec 05 '12 at 13:48
  • yes there is a stringBuilder in Android. Will try and inform. Thanks – Jaume Dec 05 '12 at 15:33
  • Not a solution because string is composed properly and problem came from ch[] that already contains residual chars. So don't mind how to compose string later if ch[] contains wrong ones – Jaume Dec 05 '12 at 15:59
  • Strange. In your real code, you sure you're respecting the 'length' you get called with? If you got `[i,m,a,g,e,2,b,e,s,t]` with `start=0 len=6`, followed by `[l,a,.,j,p,g,b,e,s,t]` with `start=0 and len=6`, all's well, because you should never read those last 4 chars. – David Bullock Dec 05 '12 at 16:07
  • sure, problem is that some chars has residual information or char array is not fulfilled. From "image2bla.jpg" we get "image2" or "image2tes". So char array is corrupted. This happens one time each 100! – Jaume Dec 05 '12 at 16:18
  • ok, that's it. Next time that characters is fired, contains remaining chars of previous call. I did some work to concat strings when endElement to solve it. Really thankful! – Jaume Dec 05 '12 at 17:30