Why is XmlPullParser delivering two START_DOCUMENT events?

孤人 提交于 2019-12-01 08:46:56

问题


In an Android app I have an XmlPullParser,

XmlResourceParser xrp = c.getResources().getXml(rid);

... (as you can see it's from an Android binary XML resource) ... and the first two events that it delivers are both START_DOCUMENT. Why? Is that normal? Intuitively, I would suppose that an XML document := one XML document, not a collection of XML documents.

My XML document /does/ have more than one root-level tag:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE STUFF OTHER STUFF "http://www.mydtd.com/mydtddtdtdtdtdtdtdtandroid">

<realRootTagOfMyDocument  version="9001">
    <otherStuff>
    ...

Does the DOCTYPE line count as another XML document?

(Here is what some test code prints:

01-15 14:11:36.000  11566-11566/com.mypkg I/System.out﹕ Start document
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ Start document -1
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ Start document null
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ xrp android.content.res.XmlBlock$Parser@42771098
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ Start document
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ Start document -1
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ Start document null
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ xrp android.content.res.XmlBlock$Parser@42771098
01-15 14:11:36.007  11566-11566/com.mypkg I/System.out﹕ Start tag realRootTagOfMyDocument

... and the test code:

private static void test(XmlResourceParser xrp) throws XmlPullParserException, IOException {
    int eventType = xrp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if(eventType == XmlPullParser.START_DOCUMENT) {
            System.out.println("Start document");
            int attributeCount = xrp.getAttributeCount();
            System.out.println("Start document " + attributeCount);
            System.out.println("Start document " + xrp.getText());
            if (attributeCount > 0)
                System.out.println("Start document " + xrp.getAttributeName(0));
            else
                System.out.println("xrp " + xrp);
        } else if(eventType == XmlPullParser.START_TAG) {
            System.out.println("Start tag " + xrp.getName());
        } else if(eventType == XmlPullParser.END_TAG) {
            System.out.println("End tag " + xrp.getName());
        } else if(eventType == XmlPullParser.TEXT) {
            System.out.println("Text " + xrp.getText());
        }
        eventType = xrp.next();
    }
    System.out.println("End document");
    throw new Error();
}

...)


回答1:


From the XMLPullParser docs:

Signalize that parser is at the very beginning of the document and nothing was read yet.

So I'd imagine that since it hasn't read anything (no start tags, end tags, end of document, etc.) it stays in the state?



来源:https://stackoverflow.com/questions/21148929/why-is-xmlpullparser-delivering-two-start-document-events

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