开发者

CDATA XML is truncated while parsing

I'm using a SAX parser (on android) to parse an xml file from an WebService. On some elements the CDATA is truncated and not complete, e.g. the XML-file contains data like

 <name><![CDATA[Gölsder开发者_高级运维 und Ginck GmbH]]></name> 

and after parsing the xml file with

public void characters(char[] ch, int start, int length)
   throws SAXException {
   super.characters(ch, start, length);
   String text = new String(ch, start, length);

the text only contains "Gölsder und Gin" (the first 15 characters). I debugged it with eclipse and i see that the whole string is not contained in the "char[] ch" argument of the method. so the parsing itself seems to have an error


I've had this problem, too. The thing is that the characters() method can be called multiple times on the same element. In your case, if you were to write this:

public void characters(char[] ch, int start, int length)
   throws SAXException {
   super.characters(ch, start, length);
   String text = new String(ch, start, length);
   Log.d("XMLTEST", text);
}

You would probably get two log messages, one Gölsder und Gin and one ck GmbH.

In conclusion, you need to have member variables that you concatenate when receiving new characters.


This is my work around after reading felix's post

private String text;

@Override
public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
        throws SAXException {
       text = null;
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    super.characters(ch, start, length);
    if(text != null){
        text += String.copyValueOf(ch, start, length);
    } else {
        text = String.copyValueOf(ch, start, length);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜