XmlPull和Sax类似,是基于流(stream)操作文件,然后根据节点事件回调开发者编写的处理程序。因为是基于流的处理,因此Xmlpull和 Sax都比较节约内存资源,不会象Dom那样要把所有节点以对橡树的形式展现在内存中。 但Xmlpull比Sax更简明,而且不需要扫描完整个流。
下面来看一个pull解析xml的例子:
<?xml version="1.0" encoding="utf-8"?>
<root>
<student id="1" group="1">
<name>张三</name>
<sex>男</sex>
<age>18</age>
<email>zhangsan@163.com</email>
<birthday>1987-06-08</birthday>
<memo>好学生</memo>
</student>
<student id="2" group="2">
<name>李四</name>
<sex>女</sex>
<age>18</age>
<email>lisi@163.com</email>
<birthday>1987-06-08</birthday>
<memo>好学生</memo>
</student>
<student id="3" group="3">
<name>小王</name>
<sex>男</sex>
<age>18</age>
<email>xiaowang@163.com</email>
<birthday>1987-06-08</birthday>
<memo>好学生</memo>
</student>
<student id="4" group="4">
<name>小张</name>
<sex>男</sex>
<age>18</age>
<email>xiaozhang@163.com</email>
<birthday>1987-06-08</birthday>
<memo>好学生</memo>
</student>
<student id="5" group="5">
<name>小明</name>
<sex>男</sex>
<age>18</age>
<email>xiaoming@163.com</email>
<birthday>1987-06-08</birthday>
<memo>好学生</memo>
</student>
</root>
对应的bean:
public class Student {
private int id;
private int group;
private String name;
private String sex;
private int age;
private String email;
private String memo;
private String birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGroup() {
return group;
}
public void setGroup(int group) {
this.group = group;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
主要的解析和现实类:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PullActivity extends Activity implements Runnable{
private TextView pullTextView ;
private LinearLayout layout;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
List<Student> lists=(List<Student>) msg.obj;
for(int i=0;i<lists.size();i++){
Student student=lists.get(i);
StringBuffer sb=new StringBuffer();
sb.append(student.getId()+" ").append(student.getName()+" ")
.append(student.getAge()+" ").append(student.getSex()+" ").append(student.getBirthday()+" ")
.append(student.getEmail()+" ").append(student.getGroup()+" ");
TextView txt=new TextView(getApplicationContext());
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
txt.setText(sb.toString());
layout.addView(txt);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pull);
pullTextView=(TextView) this.findViewById(R.id.pullTextView);
layout=(LinearLayout) this.findViewById(R.id.layout);
new Thread(this).start();
}
//pull解析xml
public List<Student> pullParseXml(){
List<Student> lists=null;
Student student=null;
try {
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
//获取XmlPullParser实例
XmlPullParser pullParser=factory.newPullParser();
InputStream in=this.getClass().getClassLoader().getResourceAsStream("student.xml");
pullParser.setInput(in,"UTF-8");
//开始
int eventType=pullParser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
String nodeName=pullParser.getName();
switch (eventType) {
//文档开始
case XmlPullParser.START_DOCUMENT:
lists=new ArrayList<Student>();
break;
//开始节点
case XmlPullParser.START_TAG:
if("student".equals(nodeName)){
student=new Student();
student.setId(Integer.parseInt(pullParser.getAttributeValue(0)));
student.setGroup(Integer.parseInt(pullParser.getAttributeValue(1)));
}else if("name".equals(nodeName)){
student.setName(pullParser.nextText());
}else if("sex".equals(nodeName)){
student.setSex(pullParser.nextText());
}else if("age".equals(nodeName)){
student.setAge(Integer.parseInt(pullParser.nextText()));
}else if("email".equals(nodeName)){
student.setEmail(pullParser.nextText());
}else if("birthday".equals(nodeName)){
student.setBirthday(pullParser.nextText());
}else if("memo".equals(nodeName)){
student.setMemo(pullParser.nextText());
}
break;
//结束节点
case XmlPullParser.END_TAG:
if("student".equals(nodeName)){
lists.add(student);
student=null;
}
break;
default:
break;
}
// 手动的触发下一个事件
eventType=pullParser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lists;
}
@Override
public void run() {
// TODO Auto-generated method stub
List<Student> lists=pullParseXml();
Message msg=handler.obtainMessage();
msg.obj=lists;
handler.sendMessage(msg);
}
}
注意xml中的<?xml version="1.0" encoding="utf-8"?> 这句话要顶格显示,不要留空隙,附则错了很难调试的。
解析后效果: