1. Pull Overview
In the Android system, the XML-related package is org. xmlpull. V1. This package not only providesXmlserializerAnd provides a pull Parser for parsing XML.Xmlpullparser
Xmlserializer does not extract XML events like xmlpullparser, but instead pushes them to the data stream outputstream or writer.
Xmlserializer provides intuitive APIs, including startdocument, enddocument, starttag, endtag, and text.
Create XML in PULL mode, apply the standard XML constructor org. xmlpull. v1.xmlserializer to create XML, org. xmlpull. v1.xmlpullparser to parse XML, You need to import the following content
Org. xmlpull. V1
Org. xmlpull. v1.xmlpullparser;
Org. xmlpull. v1.xmlpullparserexception;
Org. xmlpull. v1.xmlpullparserfactory;
Org. xmlpull. v1.xmlserializer;
SDK source code viewing path (Google Code)
Pull creates and parses XML:
2. Create XML in pull
In PULL mode, XML is created throughXmlserializerClass implementation
First, use xmlserializer to obtain the XML-created instance xmlserializer.
Next, use xmlserializer to set the output xmlserializer. setoutput, xmlserializer. startdocument ("UTF-8", null) to set XML attributes.
Then, use xmlserializer to create startdocument, starttag, text, endtag, and enddocument.
Code
/** Create XML in PULL mode */<br/> Public String pullxmlcreate () {<br/> stringwriter xmlwriter = new stringwriter (); </P> <p> person [] persons = new person [3]; // create a node person object <br/> persons [0] = new person (1, "sunboy_2050", "http://blog.csdn.net/sunboy_2050"); <br/> persons [1] = new person (2, "Baidu", "http://www.baidu.com "); <br/> persons [2] = new person (3, "Google", "http://www.google.com"); </P> <p> try {<br/> //// Method 1: Use the practical tool Android provided by Android. util. XML <br/> // xmlserializer = xml. newserializer (); </P> <p> // Method 2: Use the factory-class xmlpullparserfactory method <br/> xmlpullparserfactory factory = xmlpullparserfactory. newinstance (); <br/> xmlserializer = factory. newserializer (); </P> <p> xmlserializer. setoutput (xmlwriter); // Save the created XML </P> <p> xmlserializer. setfeature ("http://xmlpull.org/v1/doc/features.html#inde NT-output ", true); <br/> // xmlserializer. setproperty ("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", ""); // set properties <br/> // xmlserializer. setproperty ("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\ n"); <br/> xmlserializer. startdocument ("UTF-8", null); // <? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> </P> <p> xmlserializer. starttag ("", "root"); <br/> xmlserializer. attribute ("", "author", "Homer"); <br/> xmlserializer. attribute ("", "date", "2012-04-28"); </P> <p> int personslen = persons. length; <br/> for (INT I = 0; I <personslen; I ++) {<br/> xmlserializer. starttag ("", "person"); // create a person node </P> <p> xmlserializer. starttag ("", "ID"); <br/> xmlserializer. text (persons [I]. GETID () + ""); <br/> xmlserializer. endtag ("", "ID"); </P> <p> xmlserializer. starttag ("", "name"); <br/> xmlserializer. text (persons [I]. getname (); <br/> xmlserializer. endtag ("", "name"); </P> <p> xmlserializer. starttag ("", "blog"); <br/> xmlserializer. text (persons [I]. getblog (); <br/> xmlserializer. endtag ("", "blog"); </P> <p> xmlserializer. endtag ("", "person"); <br/>}</P> <p> xmlserializer. endtag ("", "root"); <br/> xmlserializer. enddocument (); </P> <p>} catch (xmlpullparserexception e) {// xmlpullparserfactory. newinstance <br/> E. printstacktrace (); <br/>} catch (illegalargumentexception e) {// xmlserializer. setoutput <br/> E. printstacktrace (); <br/>} catch (illegalstateexception e) {// xmlserializer. setoutput <br/> E. printstacktrace (); <br/>} catch (ioexception e) {// xmlserializer. setoutput <br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> savedxml (filename, xmlwriter. tostring (); <br/> return xmlwriter. tostring (); <br/>}
Running result:
3. Pull Parse XML
In PULL mode, XML is parsed throughXmlpullparserClass implementation
First, the XML-parsed instance xpp is obtained through xmlpullparser.
Then, set the input xpp. setinput (is, "UTF-8") through xpp to declare the data structure (such as the person array) for saving XML Information)
Then, parse start_document, start_tag, text, end_tag, end_document, and so on through xpp.
Code
/** Pull method, parse XML */<br/> Public String pullxmlresolve () {<br/> stringwriter xmlwriter = new stringwriter (); </P> <p> inputstream is = readxml (filename); <br/> try {<br/> /// Method 1: Use the android Utility Class. util. XML <br/> // xmlpullparser xpp = xml. newpullparser (); </P> <p> // Method 2: Use the factory-class xmlpullparserfactory method <br/> xmlpullparserfactory factory = xmlpullparserfactory. newinstance (); <br/> xmlpullparser xpp = facto Ry. newpullparser (); </P> <p> xpp. setinput (is, "UTF-8"); </P> <p> List <person> personslist = NULL; // person node for saving XML <br/> person = NULL; <br/> stringbuffer xmlheader = NULL; // Save the xml header <br/> string ele = NULL; // element flag </P> <p> int eventtype = xpp. geteventtype (); <br/> while (xmlpullparser. end_document! = Eventtype) {<br/> switch (eventtype) {<br/> case xmlpullparser. start_document: <br/> personslist = new arraylist <person> (); // initialize persons <br/> xmlheader = new stringbuffer (); // initialize xmlheader <br/> break; </P> <p> case xmlpullparser. start_tag: <br/> If ("root ". equals (xpp. getname () {<br/> string attrauthor = xpp. getattributevalue (0); <br/> string attrdate = xpp. getattributevalue (1); <br/> xmlheader. append (" Root "). append ("\ t"); <br/> xmlheader. append (attrauthor ). append ("\ t"); <br/> xmlheader. append (attrdate ). append ("\ n"); <br/>} else if ("person ". equals (xpp. getname () {<br/> person = new person (); // create a person instance <br/>} else if ("ID ". equals (xpp. getname () {<br/> ele = "ID"; <br/>} else if ("name ". equals (xpp. getname () {<br/> ele = "name"; <br/>} else if ("blog ". equals (xpp. getname () {<br/> ele = "blog "; <Br/>} else {<br/> ele = NULL; <br/>}< br/> break; </P> <p> case xmlpullparser. text: <br/> If (null! = Ele) {<br/> If ("ID ". equals (Ele) {<br/> person. setid (integer. parseint (xpp. gettext (); <br/>} else if ("name ". equals (Ele) {<br/> person. setname (xpp. gettext (); <br/>} else if ("blog ". equals (Ele) {<br/> person. setblog (xpp. gettext (); <br/>}< br/> break; </P> <p> case xmlpullparser. end_tag: <br/> If ("person ". equals (xpp. getname () {<br/> personslist. add (person); <br/> person = NULL; <br/>}< br/> ele = NULL; <br/> break; <br/>}</P> <p> eventtype = xpp. next (); // next event type <br/>}</P> <p> xmlwriter. append (xmlheader); <br/> int personslen = personslist. size (); <br/> for (INT I = 0; I <personslen; I ++) {<br/> xmlwriter. append (personslist. get (I ). tostring (); <br/>}</P> <p>} catch (xmlpullparserexception e) {// xmlpullparserfactory. newinstance <br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return xmlwriter. tostring (); <br/>}
Running result:
4. Person class
See the previous blog Android creation and parsing XML (2) -- DOM mode [4, person class]
Code download
Reference recommendations:
Org. xmlpull. V1
Pull creates XML