xml文件代码:
<apps>
<app>
<id>1</id>
<name>Google Maps</name>
<version>1.0</version>
</app>
<app>
<id>2</id>
<name>Chrome</name>
<version>2.1</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>2.3</version>
</app>
</apps>
有问题的在第十三行
java代码:
private void parseXMLWithPull(String XMLData){
try{
XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = xmlPullParserFactory.newPullParser();
xmlPullParser.setInput(new StringReader(XMLData));
//0
int eventType = xmlPullParser.getEventType();
String id = "";
String name = "";
String version = "";
//1
while(eventType != XmlPullParser.END_DOCUMENT ){
String nodeName = xmlPullParser.getName();
switch (eventType){
//start to parse
//2
case XmlPullParser.START_TAG:{
if("id".equals(nodeName)){
id = xmlPullParser.nextText();
}else if ("name".equals(nodeName)){
name = xmlPullParser.nextText();
}else if("version".equals(nodeName)){
version = xmlPullParser.nextText();
}
break;
}
//end parse
//3
case XmlPullParser.END_TAG:{
if("app".equals(nodeName)){
Log.d("MainActivity", "id is " + id);
Log.d("MainActivity","name is " + name);
Log.d("MainActivity","version is " + version);
}
break;
}
default:
break;
}
eventType = xmlPullParser.next();
}
}catch (Exception e){
e.printStackTrace();
System.out.println("failed");
}
}
------解决思路----------------------
不知楼主解析XML是通过URL方式还是本地文件解析
如果是URL,请检查清单文件中是否添加了网络访问权限:
如果是本地,请Locat调试输出 ,检查是否解析的文件找不到错误。
本案例Pull解析楼主提供的xml文档demo下载地址:
http://pan.baidu.com/s/1o6GfGfS