0

我在解析 xml 文件时遇到问题。解析时到达最终的结束标记,但永远不会遇到 END_DOCUMENT。这似乎是一个无限循环!但是,如果我将字符串结果更改为“<foo>Hello World!</foo>”,那就行了!!!

public void parseXMLData(String result) throws XmlPullParserException, IOException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput( new StringReader ( result ) );
    int eventType = xpp.getEventType();

    String prefix = null, name = null, value = null;

    while (eventType != XmlPullParser.END_DOCUMENT) {

        switch(eventType) {
        case XmlPullParser.END_DOCUMENT: Log.i(TAG, "end document");
        break;
        case XmlPullParser.START_TAG : Log.i(TAG, "start tag"); Log.i(TAG, xpp.getName());
        break;
        case XmlPullParser.END_TAG : Log.i(TAG,"end tag"); Log.i(TAG, "end "+xpp.getName());
        break;
        case XmlPullParser.START_DOCUMENT : Log.i(TAG, "start document");
        break;
        case XmlPullParser.TEXT : Log.i(TAG, "text");
        break;
        case XmlPullParser.COMMENT:
        Log.i(TAG, "COMMENT");
        break;
        case XmlPullParser.DOCDECL:
        Log.i(TAG, "DOCDECL");
        break;
        case XmlPullParser.IGNORABLE_WHITESPACE:
        Log.i(TAG, "IGNORABLE_WHITESPACE");
        break;
        }
        eventType = xpp.next();
        Log.i(TAG, "next(): "+eventType);
    }

    Log.i(TAG, "Saiu do while");

    if(eventType == XmlPullParser.END_DOCUMENT) {
        Log.i(TAG, "end document");
    }


    showResults();
    //str_forecast = xpp.getText();


    Log.i(TAG, "Chamou showResults()");
    //return xpp.getText();
}

我的 XML 资源是http://weather.yahooapis.com/forecastrss?w=455823&u=c

谢谢

4

2 回答 2

1

那么它实际上在做什么,无限运行?如果它只是没有打印出您Log.i()END_DOCUMENT案例,那是因为您已经设置了 while 循环,以便在遇到END_DOCUMENT标签时立即退出。

于 2014-02-28T21:11:33.070 回答
0

试着打电话

事件类型=xpp.next();

代替

eventType = xpp.nextToken();

于 2014-02-28T22:57:46.900 回答