Android parsing XML method (iii)

Source: Internet
Author: User

In the previous section, we used the Sax method to parse the XML document, which is event-driven. Of course, the Android event mechanism is based on the callback function. In this section, we parse the XML document in another way, which is also event-driven, just like the sax way, which is the pull method. Only the pull-mode read XML callback method returns a number.

Read the declaration of XML to return start_document;
Reads the end of XML to return end_document;
Read the start tag of XML back to Start_tag
Reads the end tag of XML to return End_tag
Text that is read to XML returns text
The result of the XML data is also the River.xml file we used earlier.

The pull style is much the same as sax, and the point is that we need to know what to do when we navigate to the tag, and we can do the same thing in this section, based on the idea of sax processing in the previous section, with the basic method:

The basic approach is to instantiate the list collection to store the data object when the pull parser navigates to the start tag of the document. When navigating to an element's start tag, the element label type is returned, and if it is a river tag, the river object needs to be instantiated, and if it is another type, the label content is obtained and the river object is given. Of course it will also navigate to the text label, but here we can not.

Based on the above explanations, we can draw the following processing of the XML document logic:

1: When navigating to xmlpullparser.start_document, you can not do the processing, of course, you can instantiate the collection object and so on.

2: When navigating to Xmlpullparser.start_tag, determine if it is a river tag, and if so, instantiate the river object and call the Getattributevalue method to get the property value in the tag.

3: When navigating to other tags, such as introduction, then determine whether the river object is empty, if not empty, then take out the contents of the introduction, Nexttext method to get the contents of the text node

4: Of course, it will be navigated to Xmlpullparser.end_tag, there will be the beginning of the end. Here we need to see if it is the river end tag, and if so, save the river object in the list collection and set the river object to null.

From the above processing logic, we can draw the following code:

Public list<river> Parse (String xmlpath) {list<river> rivers=new arraylist<river> ();        River River=null;            InputStream Inputstream=null;           Get xmlpullparser parser Xmlpullparser xmlparser = Xml.newpullparser ();            try {//get the file stream and set the encoding Method Inputstream=this.context.getresources (). Getassets (). open (Xmlpath);            Xmlparser.setinput (InputStream, "utf-8");            Get resolved to the event category, here are the start document, end document, start tag, end tag, text and so on events.         int Evttype=xmlparser.geteventtype (); Loops until the end of the document while (evttype!=xmlpullparser.end_document) {switch (evttype) {case Xmlpul                 LParser.START_TAG:String TAG = Xmlparser.getname ();                   If the river label starts, the object needs to be instantiated if (tag.equalsignorecase) {river = New River ();                  Remove some attribute values from the river tag river.setname (Xmlparser.getattributevalue (null, NAME)); River.SetLength (Integer.parseint (Xmlparser.getattributevalue (null, LENGTH))); }else if (river!=null) {//If the introduction tag is encountered, read the contents if (Tag.equalsignorecase (INTRODUCT                    ION) {river.setintroduction (Xmlparser.nexttext ());                    }else if (Tag.equalsignorecase (IMAGEURL)) {River.setimageurl (Xmlparser.nexttext ());                           }} break; Case Xmlpullparser.end_tag://If a river tag is encountered, add the river object into the collection if (Xmlparser.getname (). Equalsignore                    Case (river) && river! = null) {Rivers.add (river);                river = null;                 } break;            Default:break;         }//If the XML does not end, navigate to the next River Node Evttype=xmlparser.next (); }} catch (Xmlpullparserexception e) {//TODO auto-generated catch block E.printStackTrace ();        }catch (IOException E1) {//TODO auto-generated catch block E1.printstacktrace ();     } return rivers; }

The result is the same as the other 2:

Android parsing XML method (iii)

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.