我在解析 xml 文件时遇到问题。解析时,它会到达最终的结束标记,但永远不会满足END_DOCUMENT。这似乎是一个无限循环!!但是,如果我通过"
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
谢谢
那么它到底在做什么,无限运行呢?如果只是没有为END_DOCUMENT
情况打印出Log.i()
,那是因为您已将 while 循环设置为在遇到 END_DOCUMENT
标记时立即退出。
尝试调用
eventType=xpp.next();
而不是
eventType = xpp.nextToken();