XML parsing Technology in Android (III), pull parsing XML files

Source: Internet
Author: User

The Android system also provides another XML parsing method that allows you to better handle this situation, that is, parsing XML data using the pull method. The pull parsing XML project is an open-source project, but the Android system has concentrated on it.

Official Website: http://www.xmlpull.org

The pull parser and the SAX Parser have differences but similarities. The difference is that the SAX Parser automatically pushes events to the registered event processor for processing, so you cannot control the event processing to take the initiative to end; the pull parser works in a way that allows your applicationProgramCodeTake the initiative to get the event from the parser, because it is actively get the event, so you can not get the event after meeting the required conditions, end the parsing. This is their main difference.

While their similarity lies in the running mode, the pull parser also provides events similar to SAX (start documentStart_documentAnd end documentEnd_document, Start ElementStart_tagAnd End ElementEnd_tag, Encountered Element ContentTextBut you need to call next ()
(Actively extract events ).

In the Android system, the package related to the PULL mode is Org. xmlpull. v1: The factory class xmlpullparserfactory and pull parser of the pull parser are provided in this package. The xmlpullparserfactory instance calls the newpullparser method to create an xmlpullparser parser instance, and then the xmlpullparser instance can be called.Geteventtype ()AndNext ()And other methods to actively extract events and perform logical Processing Based on the extracted event types.

Next, let's take a look at how pull parses XML files.

Parse this simple file:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Persons> <person id = "1"> <Name> Lucy </Name> <age> 15 </age> </person> <person id = "2"> <name> Tim </Name> <age> 20 </age> </person> </persons>

Parsing Code:

Public void pullparsexml () throws xmlpullparserexception, ioexception {// construct the parser xmlpullparser parser = xml. newpullparser (); // get the input stream of the XML file and get the object inputstream = getclass (). getclassloader (). getresourceasstream ("test. XML "); parser. setinput (inputstream, "UTF-8"); // start parsing int eventtype = parser. geteventtype (); // generate the first event list <person> Persons = NULL; person = NULL; while (eventtype! = Xmlpullparser. end_document) {// The circular condition document ends switch (eventtype) {// identifies the event type case xmlpullparser. start_document: // start document persons = new arraylist <person> (); break; Case xmlpullparser. start_tag: // Start Element Node string name1 = parser. getname (); // get the name of the parser currently pointing to the element node if ("person ". equals (name1) {// if it points to the person node person = new person (); person. setid (New INTEGER (parser. getattributevalue (0); // obtain the value of the attribute whose index is 0} else if ("name ". equals (name1) {person. setname (parser. nexttext (); // obtain the value of the next text node of the current node} else if ("age ". equals (name1) {person. setage (New INTEGER (parser. nexttext (); // obtain the value of the next text node of the current node} break; Case xmlpullparser. end_tag: // End Element Node string name2 = parser. getname (); If ("person ". equals (name2) {// if it points to/person node persons. add (person); person = NULL;} break;} eventtype = parser. next (); // The parser moves down a unit} For (person P: Persons) {system. out. println (P. GETID () + "--" + P. getname () + "---" + P. getage ());}}

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.