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