XML parsing in Android

Source: Internet
Author: User

Like other systems, android can parse XML in three ways: sax, Dom, and pull. This document mainly records the PULL mode.

Pull parses XML in an event-driven manner. When pull reaches a tag, it can return an event. We parse the entire XML document based on the event.

/** <Br/> * instream indicates the input stream of the file to be parsed <br/> */<br/> Public static list <person> getpersons (inputstream instream) {<br/> List <person> Persons = NULL; <br/> person = NULL; <br/> xmlpullparser parser = xml. newpullparser (); <br/> parser. setinput (instream, "UTF-8"); <br/> int eventtype = parser. geteventtype (); // generates the first event <br/> while (eventtype! = Xmlpullparser. end_document) {// as long as it is not a document end event <br/> switch (eventtype) {<br/> case xmlpullparser. start_document: // actually <persons> <br/> Persons = new arraylist <person> (); <br/> break; </P> <p> case xmlpullparser. start_tag: // actually <person <name <age <br/> string name = parser. getname (); // get the name of the element currently pointed to by the parser <br/> If ("person ". equals (name) {<br/> person = new person (); <br/> person. setid (New INTEGER (parser. getattributeval UE (0); <br/>}< br/> If (person! = NULL) {<br/> If ("name ". equals (name) {<br/> person. setname (parser. nexttext (); // obtain the value of the parser that points to the next text node of the element <br/>}< br/> If ("age ". equals (name) {<br/> person. setage (new short (parser. nexttext (); <br/>}< br/> break; </P> <p> case xmlpullparser. end_tag: // actually </person> </age> </Name> <br/> If ("person ". equals (parser. getname () {<br/> persons. add (person); <br/> person = NULL; <br/>}< br/> break; <br/>}< br/> eventtype = parser. next (); <br/>}< br/> return persons; <br/>}

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <persons> <br/> <person id = "23"> <br/> <Name> liming </Name> <br/> <age> 30 </age> <br/> </person> <br/> <person id = "20"> <br/> <Name> lixiangmei </Name> <br/> <age> 25 </age> <br/> </person> <br/> </persons> <br/>

 

Additional use of pull to write XML:

Public static void save (list <person> persons, writer) {<br/> xmlserializer serializer = xml. newserializer (); <br/> serializer. setoutput (writer); <br/> serializer. startdocument ("UTF-8", true); </P> <p> serializer. starttag (null, "persons"); <br/> for (person: Persons) {<br/> serializer. starttag (null, "person"); <br/> serializer. attribute (null, "ID", person. GETID (). tostring (); </P> <p> serializer. starttag (null, "name"); <br/> serializer. text (person. getname (); <br/> serializer. endtag (null, "name"); </P> <p> serializer. starttag (null, "Age"); <br/> serializer. text (person. getage (). tostring (); <br/> serializer. endtag (null, "Age"); </P> <p> serializer. endtag (null, "person"); <br/>}< br/> serializer. endtag (null, "persons"); <br/> serializer. enddocument (); <br/> writer. flush (); <br/> writer. close (); <br/>}

Related Article

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.