The XML parsing pull for Android development

Source: Internet
Author: User

Parsing XML in Android is a very common operation, except for the two most commonly used parsing XML of sax and Dom, theandroid built- in pull parser parses XML file. Pull parsing is used extensively in Android's source code, and pull is not only more of a face object, but also uses more speed and efficiency .

PullThe parser is an open sourceJavaproject that can be used for bothAndroid, can also be used tojava EE. If used injava EEneed to put itsJarfile into the classpath becauseAndroidhas been integrated into the Pullparser, so there is no need to add anyJarfile. Androidthe system itself uses a variety ofXMLdocument, the interior is also used PullParsed by the parser. Pullthe way the parser runs andSAXThe parser is similar. It provides similar events, such as: start element and end element event, usingParser.next()you can go to the next element and trigger the corresponding event. WithSAXthe difference is that Pullthe event generated by the parser is a number, not a method, so you can use aSwitchhandle the event of interest. When the element begins parsing, the callParser.nexttext()method to get the nextTextthe value of the type node.

The following code shows how to use the Pull parser:

If there is a person.xml

<?xml version= "1.0" encoding= "UTF-8"? ><persons><person id= "All" ><name>liming</name> <age>30</age></person><person id= "><name>zhangxiaoxiao</name><age>" 25</age></person></persons>

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 Personservice class for reading and writing Person.xml

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<person> getpersons (InputStream xml) throws Xmlpullparserexception, ioexception {list<person> persons = NULL; Person person = null;//Xmlpullparser pullparser =//xmlpullparserfactory.newinstance (). Newpullparser (); Xmlpullparser Pullparser = Xml.newpullparser ();p ullparser.setinput (Xml, "UTF-8"); Set the XML data to parse for XML int eventtype = Pullparser.geteventtype (); while (eventtype! = xmlpullparser.end_document) {switch ( EventType) {Case XmlPullParser.START_DOCUMENT:persons = new arraylist<person> (); Break;case Xmlpullparser.start _tag:if ("Person". Equals (Pullparser.getname ())) {int id = integer.valueof (pullparser.getAttributeValue (0));p Erson = new Person ();p Erson.setid (ID);} if ("Name". Equals (Pullparser.getname ())) {String name = Pullparser.nexttext ();p erson.setname (name);} if ("Age". Equals (Pullparser.getname ())) {int. = Integer.valueof (Pullparser.nexttext ());p erson.setage (age);} Break;case XmlPullParser.END_TAG:if ("person". Equals (Pullparser.getname ())) {Persons.add (person);p Erson = null;} break;} EventType = Pullparser.next ();} return persons;} public static void Saveperson (List<person> 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"); 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 ();}}


The XML parsing pull for Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.