private List pullXml() {
//得到pull解析器
try {
XmlPullParser pullParser = Xml.newPullParser();
File file = new File("mnt/sdcard/person.xml");
FileInputStream fis = new FileInputStream(file);
pullParser.setInput(fis, "utf-8");
//得到事件
int eventType = pullParser.getEventType();
//如果事件不等于文档结束,就一直让它解析
END_DOCUMENT 文档结束
while(eventType!=XmlPullParser.END_DOCUMENT){
//得到节点的名字
String tagName = pullParser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
//开始标签
if(tagName.equals("persons")){
list = new ArrayList<>();
}else if(tagName.equals("person")){
s = new Student();
}else if(tagName.equals("name")){
//得到标签里的内容
String nextText = pullParser.nextText();
s.setName(nextText);
}else if(tagName.equals("age")){
String age = pullParser.nextText();
s.setAge(age);
}
break;
case XmlPullParser.END_TAG:
//开始标签
if(tagName.equals("person")){
//把对像加到集合
list.add(s);
s=null;
}
break;
default:
break;
}
//得到下一个事件
eventType = pullParser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}