您的位置:首页 > 其它

XML使用Pull进行解析的简单Dome

2016-06-29 11:02 537 查看
  本次介绍的是xml使用pull解析的小Demo

      以下是pull解析案例

这里展示的是本次解析的xml文件 xmltest_easy.xml

<?xml version="1.0" encoding="UTF-8"?>
<country>
<province province_name="山东" >
<college >
<name>德州学院</name>
<student>20000</student>
</college>
<college >
<name>青岛大学</name>
<student>40000</student>
</college>
</province>
<province province_name="河北">
<college >
<name>河北医科大学</name>
<student>30000</student>
</college>
</province>
</country>

这里展示的是对上方xmltest_easy.xml解析的方法

public void parseeasy(){
try {
XmlPullParserFactory Factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = Factory.newPullParser();
parser.setInput(getAssets().open("xmltest_easy.xml"), "utf-8");
int eventType = parser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
String name = parser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
list_easy=new ArrayList<easybean>();
break;
case XmlPullParser.START_TAG:

if("province".equals(name)){
easy = new easybean();
easy.setProvince(parser.getAttributeName(0));
} if("name".equals(name)){
easy.setName(parser.nextText());
} if("student".equals(name)){
easy.setStudent(parser.nextText());
} if("student".equals(name)){
easy.setStudent(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("province".equals(name)){
System.out.println("节点:"+name);
list_easy.add(easy);
}

break;
default:
break;
}

eventType = parser.next();
}

for (int i = 0; i < list_easy.size(); i++) {
System.out.println("easy:"+list_easy.get(i));
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

其中的easybean的实体类
package com.example.xmlparse.bean;

public class easybean {
private String province;
private String college;
private String name;
private String student;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public easybean(String province, String college, String name, String student) {
super();
this.province = province;
this.college = college;
this.name = name;
this.student = student;
}
@Override
public String toString() {
return "easybean [province=" + province + ", college=" + college
+ ", name=" + name + ", student=" + student + "]";
}
public easybean() {
super();
}

}


以上就是对xml使用pull解析的简单小Dome,希望能够为你提供帮助
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: