一、理论准备
Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。跟SAX不同的是, Pull解析器产生的事件是一个数字,而非方法,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型节点的值。
工程结构如下:
为什么把xml放在assets目录下呢,它和res/values目录的区别是系统不分配ID,好像没解决问题,暂时没查到资料。
二、上代码
package com.example.and_0003;
import java.io.InputStream;
import java.util.List;
import com.hpu.entity.Student;
import com.hpu.util.PullService;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {/** Called when the activity is first created. */
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);AssetManager asset = getAssets();try {
InputStream input = asset.open("student.xml");
List<Student> list = PullService.getStudents(input);for (Student stu : list) {
Log.e("StudentInfo","Person ID: " + stu.getId() + ","+ stu.getName() + ", " + stu.getAge() + ", "+ stu.getSex());}} catch (Throwable e) {
e.printStackTrace();}}}
1: <?xml version="1.0" encoding="utf-8"?>2: <students>3: <student id="20110806100">4: <name>小明</name>5: <age>22</age>6: <sex>男</sex>7: </student>8: <student id="20110806101">9: <name>小李</name>10: <age>24</age>11: <sex>男</sex>12: </student>13: <student id="20110806102">14: <name>小丽</name>15: <age>21</age>16: <sex>女</sex>17: </student>18: </students>
package com.hpu.entity; public class Student { private String id; private String name; private int age; private String sex; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
四、运行结果1: package com.hpu.util;
2:3: import java.io.InputStream;
4: import java.util.ArrayList;
5: import java.util.List;
6:7: import org.xmlpull.v1.XmlPullParser;
8: import org.xmlpull.v1.XmlPullParserFactory;
9:10: import com.hpu.entity.Student;
11:12: import android.util.Xml;
13:14: public class PullService {15:16: // 采用XmlPullParser来解析XML文件
17: public static List<Student> getStudents(InputStream inStream)18: throws Throwable {
19: List<Student> students = null;
20: Student mStudent = null;
21:22: // ========创建XmlPullParser,有两种方式=======
23: // 方式一:使用工厂类XmlPullParserFactory
24: XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance();25: XmlPullParser parser = pullFactory.newPullParser();26: // 方式二:使用Android提供的实用工具类android.util.Xml
27: // XmlPullParser parser = Xml.newPullParser();
28:29: // 解析文件输入流
30: parser.setInput(inStream, "UTF-8");
31: // 产生第一个事件
32: int eventType = parser.getEventType();
33: // 只要不是文档结束事件,就一直循环
34: while (eventType != XmlPullParser.END_DOCUMENT) {
35: switch (eventType) {
36: // 触发开始文档事件
37: case XmlPullParser.START_DOCUMENT:
38: students = new ArrayList<Student>();
39: break;
40: // 触发开始元素事件
41: case XmlPullParser.START_TAG:
42: // 获取解析器当前指向的元素的名称
43: String name = parser.getName();44: if ("student".equals(name)) {45: // 通过解析器获取id的元素值,并设置student的id
46: mStudent = new Student();
47: mStudent.setId(parser.getAttributeValue(0));48: }49: if (mStudent != null) {50: if ("name".equals(name)) {51: // 获取解析器当前指向元素的下一个文本节点的值
52: mStudent.setName(parser.nextText());53: }54: if ("age".equals(name)) {55: // 获取解析器当前指向元素的下一个文本节点的值
56: mStudent.setAge(new Short(parser.nextText()));
57: }58: if ("sex".equals(name)) {59: // 获取解析器当前指向元素的下一个文本节点的值
60: mStudent.setSex(parser.nextText());61: }62: }63: break;
64: // 触发结束元素事件
65: case XmlPullParser.END_TAG:
66: //
67: if ("student".equals(parser.getName())) {68: students.add(mStudent);69: mStudent = null;
70: }71: break;
72: default:
73: break;
74: }75: eventType = parser.next();76: }77: return students;
78: }79:80: }81:
五、遗留问题及参考文献
Dom和SAX和Pull的适用范围分别是什么?