Android development-XML parsing PULL
Parsing xml in Android is a very common operation. Apart from the most common parsing xml in SAX and DOM,Android built-in Pull parser parses XMLFile. Pull is widely used in Android source code for parsing. pull not only has more facial objects, but also has higher usage speed and efficiency..
The Pull parser is an open-source java project that can be used for both android and JavaEE. If it is used in javaEE, you need to put its jar file into the class path. Because Android has already been integrated into the Pull parser, no jar file needs to be added. Xml files used by the android system are parsed using the Pull parser. The Running Method of the Pull parser is similar to that of the SAX Parser. It provides similar events, such as the start element and end element events. You can use parser. next () to enter the next element and trigger the corresponding event. Unlike SAX, the events generated by the Pull parser are numbers rather than methods. Therefore, you can use a switch to process events of interest. When parsing an element, call parser. nextText () to obtain the value of the next Text node.
The following code demonstrates how to use the Pull Parser:
If there is a person. xml
liming
30
zhangxiaoxiao
25
Person entity:
package com.andy.domain;public class Person {private Integer id;private String name;private Integer age;public Person() {}public Person(Integer id, String name, Integer age) {super();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;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";}}
Complete the reading and writing of the person. xml PersonService class
Package com. andy. 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 com. andy. domain. person; public class PersonService {public static List
GetPersons (InputStream xml) throws XmlPullParserException, IOException {List
Persons = null; Person person = null; // XmlPullParser pullParser = // XmlPullParserFactory. newInstance (). newPullParser (); XmlPullParser pullParser = Xml. newPullParser (); pullParser. setInput (xml, "UTF-8"); // set the xml data to be parsed for xml int eventType = pullParser. getEventType (); while (eventType! = XmlPullParser. END_DOCUMENT) {switch (eventType) {case XmlPullParser. START_DOCUMENT: persons = new ArrayList
(); Break; case XmlPullParser. START_TAG: if ("person ". equals (pullParser. getName () {int id = Integer. valueOf (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 () {int age = Integer. valueOf (pullParser. nextText (); person. setAge (age);} break; case XmlPullParser. END_TAG: if ("person ". equals (pullParser. getName () {persons. add (person); person = null;} break;} eventType = pullParser. next ();} return persons;} public static void savePerson (List
Persons, OutputStream outputStream) throws IllegalArgumentException, IllegalStateException, IOException {XmlSerializer serializer = Xml. newSerializer (); serializer. setOutput (outputStream, "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 (). toString (); serializer. startTag (null, "name"); serializer. text (person. getName (); serializer. endTag (null, "name"); serializer. startTag (null, "age"); serializer. text (person. getAge (). toString (); serializer. endTag (null, "age"); serializer. endTag (null, "person");} serializer. endTag (null, "persons"); serializer. endDocument (); outputStream. flush (); outputStream. close ();}}