Pull简介
常见的XML解析方式有三种,DOM,SAX,Pull,Android系统中推荐使用Pull
Pull解析器是一个开源的Java项目,Android系统内部解析XML文件均为此种方式,也可用于J2EE项目
Android SDK中已经集成了Pull解析器,无需添加任何jar文件
Pull解析器运行方式与SAX类似,提供各种事件的判断
官方网站:http://xmlpull.org/
使用XmlPullParser解析XML文件
Xml.newPullParser()获得解析器
parser.setInput(in, "UTF-8")设置输入流以及编码
parser.next()获取下一个解析事件,得到一个事件代码
XmlPullParser中定义了常量来标识各种解析事件
START DOCUMENT START_TAG TEXT END_TAG END_DOCUMENT
使用XmlSerializer写出XML
使用以下方法生成XML,和XML文档顺序类似
startDocument
startTag
attribute
text
endTag
endDocument
第一步:新建一个Android工程命名为xml目录结构如下图:
person.xml文件放在src目录下:
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>gaolei</name> <age>23</age> </person> <person id="24"> <name>xiaoqiang</name> <age>25</age> </person> </persons>
package cn.leigo.domain; public class Person { private Integer id; private String name; private Integer age; public Person() { } public Person(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package cn.leigo.service; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import cn.leigo.domain.Person; public class PersonService { /** * 获取数据 * * @param in * @return * @throws XmlPullParserException * @throws IOException */ public static List<Person> parser(InputStream in) throws XmlPullParserException, IOException { List<Person> persons = null; Person person = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(in, "UTF-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: if ("person".equals(parser.getName())) { person = new Person(); String id = parser.getAttributeValue(0); person.setId(Integer.valueOf(id)); } else if ("name".equals(parser.getName())) { String name = parser.nextText(); person.setName(name); } else if ("age".equals(parser.getName())) { String age = parser.nextText(); person.setAge(Integer.parseInt(age)); } break; case XmlPullParser.END_TAG: if ("person".equals(parser.getName())) { persons.add(person); person = null; } break; } eventType = parser.next(); } return persons; } /** * 保存数据 * * @param persons * 数据 * @param os * 输出方向 * @throws IllegalArgumentException * @throws IllegalStateException * @throws IOException */ public static void serializer(List<Person> persons, OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(os, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for (Person person : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "id", person.getId() + ""); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(person.getAge() + ""); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); os.flush(); os.close(); } }
最后在添加一个测试类:
package cn.leigo.test; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.test.AndroidTestCase; import android.util.Log; import cn.leigo.domain.Person; import cn.leigo.service.PersonService; public class PersonServiceTest extends AndroidTestCase { private static final String TAG = "PersonServiceTest"; public void testPersons() throws Exception { InputStream in = this.getClass().getClassLoader() .getResourceAsStream("person.xml"); List<Person> persons = PersonService.parser(in); for (Person person : persons) { Log.i(TAG, person.toString()); } } public void testSave() throws Exception { List<Person> persons = new ArrayList<Person>(); persons.add(new Person(1, "zhangsan", 24)); persons.add(new Person(2, "lisi", 35)); persons.add(new Person(3, "wangwu", 43)); FileOutputStream fos = getContext().openFileOutput("persons.xml", Context.MODE_PRIVATE); PersonService.serializer(persons, fos); } }
解析Xml
生成Xml
生成的xml文件如下:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <persons> <person id="1"> <name>zhangsan</name> <age>24</age> </person> <person id="2"> <name>lisi</name> <age>35</age> </person> <person id="3"> <name>wangwu</name> <age>43</age> </person> </persons>