Pull模式解析原理:
与SAX解析器模拟相似,也提供了类似的事件驱动.使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。
Pull模式解析步骤:
1、 得到一个XmlPullParser对象这里得到XMLPullParser对象的方式有两种:
1)通过Xml这个工具类的.newPullParser()方式得到
2)通过XmlPullParserFactory工厂创建
//得到XmlPullParserFacotry工厂
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
-
- XmlPullParser xmlparser = factory.newPullParser();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlparser = factory.newPullParser();
2、 再根据XmlPullParser对象设置输入流和编码格式
- parser.setInput(inputStream, "UTF-8");
parser.setInput(inputStream, "UTF-8");
3、 然后循环遍历所有的结点可以通过文档开始的代码来判断读入到哪个节点
关于xmlparser.getEventType()其值有:
Start_DOCUMENT:文档开始
END_DOCUMENT:文档结束
START_TAG:开始读标签
END_TAG:标签结束
TEXT:所读的是文本内容
一个简单示例:
[java] view plain copy print ?
- import java.io.IOException;
- import java.io.StringReader;
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
- import org.xmlpull.v1.XmlPullParserFactory;
- public class SimpleXmlPullApp{
- public staticvoid main (String args[]) throws XmlPullParserException, IOException{
-
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
-
- xpp.setInput( new StringReader ("<foo>Hello World!</foo>" ) );
-
- int eventType = xpp.getEventType();
-
- while (eventType != XmlPullParser.END_DOCUMENT) {
-
- if(eventType == XmlPullParser.START_DOCUMENT) {
-
- System.out.println("Start document");
-
- } else if(eventType == XmlPullParser.START_TAG) {
-
- System.out.println("Start tag "+xpp.getName());
-
- } else if(eventType == XmlPullParser.END_TAG) {
-
- System.out.println("End tag "+xpp.getName());
-
- } else if(eventType == XmlPullParser.TEXT) {
-
- System.out.println("Text "+xpp.getText());
-
- }
- eventType = xpp.next();
- }
- System.out.println("End document");
- }
- }
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class SimpleXmlPullApp{
public static void main (String args[]) throws XmlPullParserException, IOException{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}
}
输出结果:
Start document
Start tag foo
Text Hello World!
End tag foo
还是以上一节的例子:读取XML文件里的内容把这些内容构造成一个Person对象,然后读取这个XML文档里内容将其Person对象添加到一个List集合里
person.xml:
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="UTF-8"?>
- <persons>
- <personid="23">
- <name>李明</name>
- <age>30</age>
- </person>
- <personid="20">
- <name>李向梅</name>
- <age>25</age>
- </person>
- </persons>
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>李明</name>
<age>30</age>
</person>
<person id="20">
<name>李向梅</name>
<age>25</age>
</person>
</persons>
Person类:
[java] view plain copy print ?
- public class Person {
-
- private Integer id;
- private String name;
- private short age;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public short getAge() {
- return age;
- }
- public void setAge(short age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person [age=" + age +", id=" + id + ", name=" + name +"]";
- }
- }
public class Person {
private Integer id;
private String name;
private short age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getAge() {
return age;
}
public void setAge(short age) {
this.age = age;
}
@Override
public String toString() {
return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
}
}
业务bean
[java] view plain copy print ?
- public static List<Person> readXml(InputStream inputStream)
-
- throws Exception {
-
- List<Person> persons=null;
-
-
-
- XmlPullParser parser = Xml.newPullParser();
-
-
-
- parser.setInput(inputStream, "UTF-8");
-
-
-
- int eventCode = parser.getEventType();
-
- Person person = null;
-
- while (eventCode != XmlPullParser.END_DOCUMENT) {
-
- switch (eventCode) {
-
-
-
- case XmlPullParser.START_DOCUMENT:
-
-
-
- persons = new ArrayList<Person>();
-
- break;
-
- case XmlPullParser.START_TAG:
-
- if ("person".equals(parser.getName())) {
-
- person = new Person();
-
- person.setId(Integer.parseInt(parser.getAttributeValue(0)));
-
- }
-
- else if(person!=null){
-
- if ("name".equals(parser.getName())) {
-
-
-
- person.setName(parser.nextText());
-
- }
- else if("age".equals(parser.getName())){
- person.setAge(new Short(parser.nextText()));
-
- }
- }
- break;
- case XmlPullParser.END_TAG:
-
- if ("person".equals(parser.getName()) && person!=null) {
-
- persons.add(person);
-
- person=null;
-
- }
-
- break;
-
- }
-
- eventCode = parser.next();
- }
- return persons;
- }
public static List<Person> readXml(InputStream inputStream)
throws Exception {
List<Person> persons=null;
// XMLPullParser对象
XmlPullParser parser = Xml.newPullParser();
//设置输入流和编码
parser.setInput(inputStream, "UTF-8");
// 文件开始代码
int eventCode = parser.getEventType();
Person person = null;
while (eventCode != XmlPullParser.END_DOCUMENT) {
switch (eventCode) {
//文档开始
case XmlPullParser.START_DOCUMENT:
// 初始化
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG: //开始标签
if ("person".equals(parser.getName())) {
person = new Person();
person.setId(Integer.parseInt(parser.getAttributeValue(0)));
}
else if(person!=null){
if ("name".equals(parser.getName())) {
//nextNext()如果下一个元素的内容是文本的时候适合用
person.setName(parser.nextText());
}
else if("age".equals(parser.getName())){
person.setAge(new Short(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG://结束标签
if ("person".equals(parser.getName()) && person!=null) {
persons.add(person);
person=null;//再掷为null
}
break;
}
// 下一个节点
eventCode = parser.next();
}
return persons;
}
测试:
[java] view plain copy print ?
- public class SAXPersonServiceTestextends AndroidTestCase {
-
- private staticfinal String TAG = "LogTest";
-
-
- public void testPULLReadXml()throws Exception {
-
- System.out.println("testPULLReadXml");
-
- InputStream inputStream = SAXPersonServiceTest.class.getClassLoader()
-
- .getResourceAsStream("person.xml");
-
- List<Person> list = PULLPersonService.readXml(inputStream);
-
- System.out.println(list.size());
-
- for (Person person : list) {
- System.out.println(person);
- }
-
- }
-
- }