除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android内置的Pull解析器解析XML文件。Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事
件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进
行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型节点的值。
使用Pull解析器读取itcast.xml的代码在本页下方备注
Pull解析器的源码及文档下载网址:http://www.xmlpull.org/
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中;或者使用DOM
API生成XML文件,或者也可以使用pull解析器生成XML文件,这里推荐大家使用Pull解析器。
xml语法:
xml字符数组装载:
2、读取的xml文件,Person.xml
<?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>
package cn.huangjie.domain; 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; } public Person(){} public Person(Integer id, String name, Short age) { this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package cn.huangjie.xml; import android.app.Activity; import android.os.Bundle; public class XmlActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
package cn.huangjie.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.XmlSerializer; import android.util.Xml; import cn.huangjie.domain.Person; public class PersonService { /** * 读取xml文件 * @param xml * @return * @throws Exception */ public static List<Person> getPersons(InputStream xml) throws Exception{ //两种方式获取pullParser // XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser(); XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(xml, "UTF-8");//为pull解析器设置要解析的xml数据和编码 List<Person> persons = null; Person person = null; int event = pullParser.getEventType(); while(event != XmlPullParser.END_DOCUMENT){//判断是否到达文件的结束 switch(event){ case XmlPullParser.START_DOCUMENT://文档开始事件,可以进行数据初始化处理 persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG://开始元素事件如<name> if("person".equals(pullParser.getName())){ Integer id = new Integer(pullParser.getAttributeValue(0)); person = new Person(); person.setId(id); } if("name".equals(pullParser.getName())){ String name = pullParser.nextText(); person.setName(name); } if("age".equals(pullParser.getName())){ Short age = new Short(pullParser.nextText()); person.setAge(age); } break; case XmlPullParser.END_TAG://结束标签如</name> if("person".equals(pullParser.getName())){ persons.add(person); person = null; } break; } //当进入一个节点后,若要进入下一个节点,<name>和</name>为两个节点 //必须调用pullParser.next()方法才能触发进入下一个节点 event = pullParser.next(); } xml.close(); return persons; } /** * 生成xml文件 * @throws IOException */ public static void save(List<Person> persons, OutputStream out) throws IOException{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true);//true表示文档独立 serializer.startTag(null, "persons"); for(Person person : persons){ serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(person.getId())); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(String.valueOf(person.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); out.flush();//out进行flush防止用户传入的字节流是缓冲输出流 out.close(); } }
package cn.huangjie.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.test.AndroidTestCase; import android.util.Log; import cn.huangjie.domain.Person; import cn.huangjie.service.PersonService; public class PersonServiceTest extends AndroidTestCase{ public void testPersons() throws Exception{ InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml"); Log.i("xmlInp", xml.toString()); List<Person> persons = PersonService.getPersons(xml); for(Person person : persons){ Log.i("PerSonService", person.toString()); } } public void testSave() throws IOException{ List<Person> persons = new ArrayList<Person>(); Person person1 = new Person(1, "张三", (short) 30); Person person2 = new Person(2, "李四", (short) 40); persons.add(person1); persons.add(person2); //得到该应用下的文件夹 File xmlFile = new File(getContext().getFilesDir(), "out.xml"); FileOutputStream outStream = new FileOutputStream(xmlFile); PersonService.save(persons, outStream); } }
生成xml所在路径:
输出结果: