android sax parser incomplete char array

好久不见. 提交于 2020-01-06 07:24:27

问题


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:


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


来源:https://stackoverflow.com/questions/13723855/android-sax-parser-incomplete-char-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!