Android开发8——利用pull解析器读写XML文件
一、基本介绍
对XML解析有SAX和DOM等多种方式,Android中极力推荐xmlpull方式解析xml。xmlpull不仅可用在Android上同样也适用于javase,但在javase环境中需自己获取xmlpull所依赖的类库,kxml2-2.3.0.jar,xmlpull_1_1_3_4c.jar。
jar包下载网址
http://www.xmlpull.org/
http://kxml.sourceforge.net/
二、例子
读取到xml的声明返回数字0 START_DOCUMENT;
读取到xml的结束返回数字1 END_DOCUMENT ;
读取到xml的开始标签返回数字2 START_TAG
读取到xml的结束标签返回数字3 END_TAG
读取到xml的文本返回数字4 TEXT
<?xml version="1.0" encoding="UTF-8"?><people><person id="001"><name>XY1</name><age>22</age></person><person id="002"><name>XY2</name><age>22</age></person></people>
package cn.xy.service;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.XmlSerializer;import android.util.Xml;import cn.xy.model.Person;/** * 利用pull解析器解析XML * * @author XY * */public class PersonService{/** * 从XML文件中读取数据 * * @param xml XML文件输入流 */public List<Person> getPeople(InputStream xml) throws Exception{List<Person> lst = null;Person person = null;// 利用ANDROID提供的API快速获得pull解析器XmlPullParser pullParser = Xml.newPullParser();// 设置需要解析的XML数据pullParser.setInput(xml, "UTF-8");// 取得事件int event = pullParser.getEventType();// 若为解析到末尾while (event != XmlPullParser.END_DOCUMENT) // 文档结束{String nodeName = pullParser.getName();switch (event){case XmlPullParser.START_DOCUMENT: // 文档开始lst = new ArrayList<Person>();break;case XmlPullParser.START_TAG: // 标签开始if ("person".equals(nodeName)){String id = pullParser.getAttributeValue(0);person = new Person();person.setId(id);}if ("name".equals(nodeName)){String name = pullParser.nextText();person.setName(name);}if ("age".equals(nodeName)){int age = Integer.valueOf(pullParser.nextText());person.setAge(age);}break;case XmlPullParser.END_TAG: // 标签结束if ("person".equals(nodeName)){lst.add(person);person = null;}break;}event = pullParser.next(); // 下一个标签}return lst;}/** * 向XML写入数据 * * @param os * @param person * @throws Exception */public void saveDataToXML(OutputStream os, List<Person> lst) throws Exception{XmlSerializer xs = Xml.newSerializer();xs.setOutput(os, "UTF-8");xs.startDocument("UTF-8", true);xs.startTag(null, "people");for (Person p : lst){xs.startTag(null, "person");xs.attribute(null, "person", p.getId());xs.startTag(null, "name");xs.text(p.getName());xs.endTag(null, "name");xs.startTag(null, "age");xs.text(p.getAge().toString());xs.endTag(null, "age");xs.endTag(null, "person");}xs.endTag(null, "people");xs.endDocument();os.flush();os.close();}}
public class TestClass extends AndroidTestCase{public void testPeople() throws Exception{PersonService ps = new PersonService();InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");List<Person> lst = ps.getPeople(xml);Assert.assertEquals("XY1", lst.get(0).getName());}public void testSave() throws Exception{PersonService ps = new PersonService();List<Person> lst = new ArrayList<Person>();lst.add(new Person("0001", "XY0001", 20));lst.add(new Person("0002", "XY0002", 20));File xmlFile = new File(this.getContext().getFilesDir(), "xy.xml"); // data/data/package name/filesOutputStream os = new FileOutputStream(xmlFile);ps.saveDataToXML(os, lst);}}