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