Android-xml analysis of Pull type code parsing

Source: Internet
Author: User

Because the previous project used XML file parsing, but in the use of the time is found from the Internet code, a little modification on the use of, however, the principle does not know why, so again want to use the time, the feeling is empty, do not know how to start, in order to further understand the XML file parsing, I do some understanding from the point of view of the code implementation, but when I look at the code, some of the code implementation is still not able to read, or too esoteric. I will write something as a record in my opinion.

There are three parsing of XML in Android, namely: SAX (Simple API XML), DOM (Document Object model), and the type of pull we're going to talk about today, and it's the way Android is officially advocating.

Before I speak, I'll write a general usage, such as parsing the following XML document:


<?xml version= "1.0" encoding= "UTF-8"?><students> <student   id= "1" >      <name>cc</ name>      <age>10</age>      <grade>100</grade>   </student>   <student Id= "2" >      <name>cy</name>      <age>11</age>      <grade>89</grade>   <student></students>

General parsing means, Java logic code is as follows (code interception and network):

public class Testpullxml {public list<person> getpersons (InputStream instream) throws Exception {List&lt ;        person> persons = NULL;        person person = null; Xmlpullparser parser = Xml.newpullparser ();//Get Pull Parser parser.setinput (instream, "UTF-8");//Set the encoded int EV of the input stream Enttype = Parser.geteventtype ();//Gets the first event type while (eventtype! = xmlpullparser.end_document) {//If the event type is not                    The document ends with the event switch (eventtype) {case xmlpullparser.start_document://if it is a document start event                persons = new arraylist<person> (); Create a person collection break;                    Case (Xmlpullparser.start_tag)://If you encounter a label start String tagName = Parser.getname ();//Get the name of the current element of the parser if ("Person". Equals (TagName)) {//If the current tag name is <person> person = new person ();//Create a PE Rson//assigns the attribute value of the element to the ID Person.setid (The new Integer (Parser.getattriButevalue (0))); if (person! = NULL) {//If the person has created complete if ("Name". Equals (TagName))//If the current node tag is NA                        Me Person.setname (New String (Parser.nexttext ()));                    else if ("Age". Equals (TagName))//If the current element node tag is person.setage (new short (Parser.nexttext ()));                } break;                        Case (Xmlpullparser.end_tag)://If you encounter a label End if ("person". Equals (Parser.getname ())) {//If the person tag ends                    Persons.add (person);//will create the completed person into the collection person = null;//and Empty}            Break    } eventtype=parser.next ();//Enter the next event processing} return persons; }

Below, we make a branch understanding of the above code:


Xmlpullparser parser = Xml.newpullparser ();//Get Pull Parser
First find the Newpullparser method under the XML class:



/**  * Returns A new pull parser with namespace support.  *    /public static Xmlpullparser Newpullparser () {        try {            Kxmlparser parser = new Kxmlparser ();            Parser.setfeature (Xmlpullparser.feature_process_docdecl, true);            Parser.setfeature (Xmlpullparser.feature_process_namespaces, true);            return parser;        } catch (Xmlpullparserexception e) {            throw new Assertionerror ();        }    }

from the code above, we can see that the function actually returns a class that implements the Xmlpullparser interface kxmlparser , and set the namespace and document declaration for the object, so, for the future parser object, we need to analyze the implementation code, we will find Kxmlparser this class.

Parser.setinput (instream, "UTF-8");//Set the encoding of the input stream
In this step, let's take a look at what the official documentation explains:


Set the input source for parser to the given reader and resets the parser. The event type is set to the initial value start_document. Setting the reader to null would just stop parsing and reset parser State, allowing the parser to free internal resources s Uch as parsing buffers.



I believe you can understand the effect of this method, but let's look at its implementation code:


Public part  starts    here ... public void SetInput (Reader reader) throws xmlpullparserexception {        this.reader = reader;        line = 1;        column = 0;        Type = start_document;        name = NULL;        namespace = null;        degenerated = false;        Attributecount =-1;        encoding = NULL;        Version = NULL;        standalone = null;        if (reader = = null)            return;        Srcpos = 0;        Srccount = 0;        Peekcount = 0;        depth = 0;        Entitymap = new hashtable<string, string> ();        Entitymap.put ("Amp", "&");        Entitymap.put ("apos", "'");        Entitymap.put ("GT", ">");        Entitymap.put ("LT", "<");        Entitymap.put ("quot", "\" ");    }

In this method, we initialize a lot of member variables that need to be used in the XML reading process, such as type, initialize to start_document, input stream instream, which are all necessary for parsing, so at the beginning, make sure to call the method. Sets the input source.

int eventtype = Parser.geteventtype ();//Get first event type



First of all, we need to know several types of XML events:

Start_document, Start_tag, T EXT, End_tag, end_document
/****************************** will write again tomorrow **************************************/






Android-xml analysis of Pull type code parsing

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.