问题
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