为什么XmlPullParser传递两个START

为什么XmlPullParser传递两个START

本文介绍了为什么XmlPullParser传递两个START_DOCUMENT事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Android应用中,我有一个 XmlPullParser

In an Android app I have an XmlPullParser,

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

...(您可以从Android二进制XML资源中看到)...,并且它传递的前两个事件都是 START_DOCUMENT .为什么?那是正常的吗?凭直觉,我假设一个XML文档:=一个XML文档,而不是XML文档的集合.

... (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.

我的XML文档/does/具有多个根级别标签:

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>
    ...

DOCTYPE行是否算作另一个XML文档?

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

...和测试代码:

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

...)

推荐答案

来自 XMLPullParser 文档:

因此,我想像一下,由于它什么也没读(没有开始标签,结束标签,文档末尾等),因此它保持状态?

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?

这篇关于为什么XmlPullParser传递两个START_DOCUMENT事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:24