xmlPullParser未达到END_DOCUMENT eventType

时间:2014-02-28 20:25:02

标签: android android-xmlpullparser

我在解析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

谢谢

2 个答案:

答案 0 :(得分:1)

那它究竟在做什么,无限奔跑?如果仅仅为Log.i()案例打印END_DOCUMENT,那是因为您设置了while循环,以便在遇到END_DOCUMENT标记时立即退出。

答案 1 :(得分:0)

尝试拨打

EVENTTYPE = xpp.next();

与其使用

eventType = xpp.nextToken();