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));

}

1 Answer 1

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();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

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.
yes there is a stringBuilder in Android. Will try and inform. Thanks
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
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.
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!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.