Using Android SAXParser, one of my XML elements mysteriously breaks to half

And its not ‘&’

Im using the SAXParser object do parse the actual XML.

This is normally done by passing a URL to the XMLReader.Parse method. Because my XML is coming from a POST request to a webservice, I am saving that result as a String and then employing StringReader / InputSource to feed this string back to the XMLReader.Parse method.

However, something strange is happening at the 2001st character of the XMLstring.

The ‘characters’ method of the document handler is being called TWICE in between the startElement and endElement methods, effectively breaking my string (in this case a project title) into two pieces. Because I am instantiating objects in my characters method, I am getting two objects instead of one.

This line, about 2000 chars into the string fires ‘characters’ two times, breaking between “Lower” and “Level”


When I bypass the StringReader / InputSource workaround and feed a flat XML file to XMLReader.Parse, it works absolutely fine.

Something about StringReader and or InputSource is somehow screwing this up.

Here is my method that takes and XML string and parses is through the SAXParser.

public void parseXML(String XMLstring) {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        // Something is happening in the StringReader or InputSource
        // That cuts the XML element in half at the 2001 character mark.

        StringReader sr = new StringReader(XMLstring);
        InputSource is = new InputSource(sr);

    } catch (IOException e) {
        Log.e("CMS1", e.toString());
    } catch (SAXException e) {
        Log.e("CMS2", e.toString());
    } catch (ParserConfigurationException e) {
        Log.e("CMS3", e.toString());

I would greatly appreciate any ideas on how to not have ‘characters’ firing off twice when I get to this point in the XML String.

Or, show me how to use a POST request and still pass off the URL to the Parse function.


As donroby said it’s perfectly legitimate for the parser to call the characters method more than once between startElement and endElement. However that isn’t “misbehaving” at all and you shouldn’t try to finagle things so that it doesn’t happen. Your parser seems to be using a 2000-character buffer, but there are other reasons it might break a text node into parts.

What you should do is to accumulate data in the characters method and process it later, in the endElement method when you are sure you have accumulated all of the character data for the node.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Using Android SAXParser, one of my XML elements mysteriously breaks to half

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录