2

我正在尝试从 Android 解析一些 XML。下面是 XML 的一些示例:

<updates at="1342481821" found="616" sorting="latest_updates" showing="Last 4D">
<show>
    <id>
        3039
    </id>
    <last>
        -14508
    </last>
    <lastepisode>
        -14508
    </lastepisode>
</show>
<show>
    <id>
        30612
    </id>
    <last>
        -13484
    </last>
    <lastepisode>
        163275
    </lastepisode>
</show>   

等等....

这是实际的代码:

try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
    URL url = new URL("http://services.tvrage.com/feeds/last_updates.php?hours=" + hours);
    InputStream stream = url.openStream();
    xpp.setInput(stream, null);
    int eventType = xpp.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_DOCUMENT) {
        } 
        else if (eventType == XmlPullParser.END_DOCUMENT) {
        } 
        else if (eventType == XmlPullParser.START_TAG) {
            if (xpp.getName().equalsIgnoreCase("id")) {
                showIds.add(Integer.parseInt(xpp.nextText()));
            } 
            else if (eventType == XmlPullParser.END_TAG) {
            } 
            else if (eventType == XmlPullParser.TEXT) {
            }
            eventType = xpp.next();
        }
    }
} catch   

... 等等

这没有结果,并且eventType始终0在调试器中。示例 URL 在浏览器中运行良好。有任何想法吗?

4

1 回答 1

3

你只xpp.next()eventType == XmlPullParser.START_TAG. 将xpp.next其向下移动一行,以便它始终执行。

while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_DOCUMENT) {
        } 
        else if (eventType == XmlPullParser.END_DOCUMENT) {
        } 
        else if (eventType == XmlPullParser.START_TAG) {
            if (xpp.getName().equalsIgnoreCase("id")) {
                showIds.add(Integer.parseInt(xpp.nextText()));
            } 
            else if (eventType == XmlPullParser.END_TAG) {
            } 
            else if (eventType == XmlPullParser.TEXT) {
            }
            // eventType = xpp.next(); <-- remove it
        }
        eventType = xpp.next(); // <-- move here
    }
} catch  
于 2012-07-17T16:15:03.797 回答