Using the pull parser to generate XML files and read XML files under Android

Source: Internet
Author: User
Tags tagname

The 1,pull parser, which uses the pull parser under Android, works like a sax parser. It provides similar events, such as: start element and end element event. 2, use Parser.next () to enter the next element and trigger the corresponding event. 3, the event is sent as an int value, so a switch can be used to process the corresponding event. 4, when the element begins parsing, call the Parser.nexttext () method to get the value of the next text type node. 5, related API: Get the current node event type: Parser.geteventtype (); Get the next node event type: Parser.next (); get tag attribute value: Parser.getattributevalue (); Get the text content behind the label: Parser.nexttext (); Example: <?xml version= "1.0" encoding= "UTF-8"? ><persons><person id= "" > <name>allen</name><age>36</age></person><person id= "><name>james" The </name><age>25</age></person></persons>//uses the pull parser to generate the XML file and writes to the local private void Writexmltolocal () {list<person> personlist = Getpersonlist ();//Get serialized object XmlSerializer serializer = Xml.newserializer ();//xmlserializer serializer = Xmlpullparserfactory.newinstance (). Newserializer (); try {File path = New File (Environment.getexternalstoragedirectory (), "Persons.xml"); FileOutputStream fos = new FileOutputStream (path);//Specify the location and encoding of the output of the serialized object Serializer.setoutput(Fos, "utf-8");//write Start <?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '? >serializer.startdocument ("Utf-8", true); Serializer.starttag (null, "persons");//<persons>for (person person:personlist) {// Start writing Personserializer.starttag (null, "person");//<person>serializer.attribute (NULL, "id", string.valueof ( Person.getid ()));//write person's Nameserializer.starttag (null, "name");//<name>serializer.text (Person.getname () ); Serializer.endtag (null, "name");//</name>//Write person's ageserializer.starttag (null, "age");//<age> Serializer.text (String.valueof (Person.getage ())) Serializer.endtag (null, "age");//</age>serializer.endtag (NULL, "person");//</person>}serializer.endtag (NULL, "persons");//</persons>serializer.enddocument () ;//end} catch (Exception e) {e.printstacktrace ();}} Use the pull parser to read the local XML file Private list<person> parserxmlfromlocal () {try {file path = new file ( Environment.getexternalstoragedirectory (), "Persons.xml"); FileInputStream fis = new FileInputStream (path);//Get Pull Parser object Xmlpullparser parser = Xml.newpullparser ();//xmlpullparser parser = Xmlpullparserfactory.newinstance (). Newpullparser (); Specify the parsed file and encoding format parser.setinput (FIS, "utf-8");//Get event type int eventtype = Parser.geteventtype (); List<person> personlist = null; person person = null; String Id;while (EventType! = xmlpullparser.end_document) {//Gets the name of the current node string tagName = Parser.getname (); switch ( EventType) {case Xmlpullparser.start_tag://Current equals start node <person>if ("Persons". Equals (TagName)) {//<persons> Personlist = new arraylist<person> ();} else if ("person". Equals (TagName)) {//<person id= ' 1 ">person = new Person (); id = parser.getattributevalue (NULL," ID ");p Erson.setid (integer.valueof (ID));} else if ("name". Equals (TagName)) {//<name>person.setname (Parser.nexttext ());} else if ("Age". Equals (TagName)) { <age>person.setage (Integer.parseint (Parser.nexttext ()));} Break;case xmlpullparser.end_tag://</persons>if ("person". equALS (TagName)) {//need to add the person object with the value set above to the collection personlist.add (person);} Break;default:break;} Get the next event type EventType = Parser.next ();} return personlist;} catch (Exception e) {e.printstacktrace ();} return null;}}

Using the pull parser to generate XML files and read XML files under Android

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.