1.Pull Introduction
The pull parser is built into the Android system, and the pull parser, like the SAX parser, provides similar events, such as the start element and the intro element event, using Parser.next () to go to the next element and trigger the corresponding event, and then handle it accordingly. , when the element begins parsing, the Perser.nexttext () method is invoked to get the value of the next text type element.
2.pull Features
(1) Simple structure, one interface, one another, a factory composed of the pull parser
(2) Simple to use, pull parser has only one important method next (), he is used to retrieve the next event, and his events are only five, start_document, Start_tag, TEXT, End_tag, end_document
(3) Minimal memory consumption, pull parser and SAX parser, take up less memory, but Sax parsing is a little cumbersome, Dom is very memory, so pull is recommended to use
3.SRC Structure
the project package name is Com.pullxml.mypull and the Person.xml file exists under the SRC root directory
--Com.pullxml.util
----Person.java--
com.pullxml.mypull
----Mainacitivity.java
-- Com.pullxml.service
----Pullservice.java--
com.pullxml.test
----Pulltester.java--
person.xml
4. Sample Pull parsing xml
first create a new android.xml in the SRC directory
<?xml version= "1.0" encoding= "UTF-8"?>
<persons>
<person id= "A" >
<name> xiaanming</name>
<age>23</age>
</person>
<person id= ">
<" name>liudehua</name>
<age>28</age>
</person>
</persons>
Create a new Pullxmlservice
Package com.example.pull_parser;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Org.xmlpull.v1.XmlPullParser;
Import Android.util.Log;
Import android.util.Xml; public class Pullxmlservice {public static list<person> ReadXML () throws exception{//Get android.xml under the SRC directory
The input stream of the file InputStream is = PullXMLService.class.getClassLoader (). getResourceAsStream ("Android.xml");
Used to hold the resolved person object list<person> persons = NULL;
A token Boolean flag = FALSE;
person person = null;
Instantiate a Xmlpullparser object Xmlpullparser parser = Xml.newpullparser ();
Set input stream and encoded parser.setinput (IS, "UTF-8");
Triggers the first event, according to the syntax of the XML, that is, from the beginning of his understanding of the document int eventcode = Parser.geteventtype (); If the obtained event code is the end of the document, then the parse end while (EventCode!= xmlpullparser.end_document) {switch (EventCode) {case XML pullparser.start_document:{//Start parsing when we generally do some initialization of the operation of PERsons = new arraylist<person> ();
Break
The case xmlpullparser.start_tag:{//Determines whether the current element is the element that needs to be retrieved if ("person". Equals (Parser.getname ()) {
Flag = true;
person = new person ();
Person.setid (integer.valueof (parser.getattributevalue (0)));
} if (flag) {if ("name". Equals (Parser.getname ())) {Person.setname (Parser.nexttext ());
}else if ("Age". Equals (Parser.getname ())) {Person.setage (integer.valueof (Parser.nexttext ()));
}} break;
Case xmlpullparser.end_tag:{if (' Person '. Equals (Parser.getname ()) && person!= null) {
Flag = false;
Persons.add (person);
LOG.E ("Log", person.tostring ());
person = null;
} break;
}//This step is important, the method returns an event code and is also the method that triggers the next event EventCode = Parser.next ();
return persons;
}
}