Example to parse XML using pull parser in Android _android

Source: Internet
Author: User

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;    
  } 
} 
 

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.