Android pull parses xml files and androidpull parses xml files

Source: Internet
Author: User

Android pull parses xml files and androidpull parses xml files

This article describes how to use pull in android to parse xml files.

First, write an xml file to save some weather information.

<? Xml version = "1.0" encoding = "UTF-8"?> <Weather> <city> <name> Guangzhou </name> <temp> 35 </temp> <pm> 23 </pm> </city> <name> shanghai </name> <temp> 30 </temp> <pm> 26 </pm> </city> <name> Beijing </name> <temp> 29 </temp> <pm> 55 </pm> </city> </weather>

When defining a City class

package com.wuyudong.pullparser.domain;public class City {    private String name;    private String tmp;    private String pm;        @Override    public String toString() {        return "City [name=" + name + ", tmp=" + tmp + ", pm=" + pm + "]";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getTmp() {        return tmp;    }    public void setTmp(String tmp) {        this.tmp = tmp;    }    public String getPm() {        return pm;    }    public void setPm(String pm) {        this.pm = pm;    }    }

Get xml file

InputStream is = getClassLoader (). getResourceAsStream ("weather. xml ");

Get pull parser

XmlPullParser xp = Xml. newPullParser ();

Start Parsing

Obtains the event type of the current node where the pointer is located.

Int type = xp. getEventType ();

* There are five main event types
* START_DOCUMENT: Event Type of the xml Header
* END_DOCUMENT: Event Type ending with xml
* START_TAG: The Event Type of the Start Node.
* END_TAG: The Event Type of the end node.
* TEXT: The Event Type of the TEXT node.

If the retrieved event type is not END_DOCUMENT, the parsing is not complete. If yes, the parsing is complete and the while loop ends.

While (type! = XmlPullParser. END_DOCUMENT)
* When parsing to different nodes, we need to perform different operations, so we can determine the name of the current node.
* When it is parsed to the Start Node of weather, a new list is generated.
* When the city object is parsed to the Start Node of the city, the city object is created to save the text to be parsed more conveniently.
* When the node starts to be parsed to the name, the text content of the next node is obtained. The same is true for temp and pm.

Case XmlPullParser. START_TAG:
// Obtain the name of the current node
If ("weather". equals (xp. getName ())){
Citys = new ArrayList <City> ();
}
Else if ("city". equals (xp. getName ())){
City = new City ();
}
Else if ("name". equals (xp. getName ())){
// Obtain the text of the next node of the current node
String name = xp. nextText ();
City. setName (name );
}
Else if ("temp". equals (xp. getName ())){
String temp = xp. nextText ();
City. setTemp (temp );
}
Else if ("pm". equals (xp. getName ())){
String pm = xp. nextText ();
City. setPm (pm );
}
Break;

When it is resolved to the End Node of the city, it means that all the three subnodes of the city have been parsed, and the city object is added to the list

Case XmlPullParser. END_TAG:
If ("city". equals (xp. getName ())){
Citys. add (city );
}

The complete code is as follows:

Package com. wuyudong. pullparser; import java. io. inputStream; import java. util. arrayList; import java. util. list; import org. xmlpull. v1.XmlPullParser; import com. wuyudong. pullparser. domain. city; import android. OS. bundle; import android. app. activity; import android. util. xml; import android. view. view; public class MainActivity extends Activity {List <City> citys; @ Override protected void onCreate (Bundle savedIns TanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {// get the input stream InputStream of the xml file is = getClassLoader (). getResourceAsStream ("weather. xml "); // get the pull parser XmlPullParser xp = Xml. newPullParser (); try {xp. setInput (is, "UTF-8"); // obtain the event type of the current node. int type = xp. getEventType (); City city = null; while (type! = XmlPullParser. END_DOCUMENT) {switch (type) {case XmlPullParser. START_TAG: // get the name of the current node if ("weather ". equals (xp. getName () {citys = new ArrayList <City> ();} else if ("city ". equals (xp. getName () {city = new City ();} else if ("name ". equals (xp. getName () {// obtain the text String name = xp for the next node of the current node. nextText (); // Save the text to the city object. setName (name);} else if ("temp ". equals (xp. getName () {String temp = xp. nextText (); city. setTmp (temp);} else if ("pm ". equals (xp. getName () {String pm = xp. nextText (); city. setPm (pm);} break; case XmlPullParser. END_TAG: if ("city ". equals (xp. getName () {citys. add (city) ;}break; default: break;} // move the pointer to the next node and return the event type of this node = xp. next () ;}} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();} for (City city: citys) {System. out. println (city. toString ());}}}

Running Program Results

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.