Android Beginner Tutorial: Pull parsing in Android

Source: Internet
Author: User

There are many ways of parsing in Android. There are XML parsing and JSON parsing in the general direction. And, detailed points, XML and JSON parsing each have their own many analytic way. Today this article mainly introduces pull parsing in XML parsing. As for the parsing of XML, I have written in javaweb some knowledge of Dom and dom4j and so on parsing way. Interested readers can go to Javaweb to find the relevant content.

Customize a data source first, assuming that the XML data file name returned by the access server is Weather.xml:

<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><weather><city><name> Beijing </name ><temp>5°</temp><pm>80</pm></city><city><name> XI ' an </name>< temp>-5°</temp><pm>800</pm></city><city><name> Nanjing </name><temp >12°</temp><pm>60</pm></city></weather>

For this, create a JavaBean to save the upper data later. Each city represents an object. This is more in line with the idea of object-oriented

In Mainactivity, the code completes the parsing process:

Package Com.itydl.pullparser;import Java.io.inputstream;import Java.util.arraylist;import java.util.List;import Org.xmlpull.v1.xmlpullparser;import Org.xmlpull.v1.xmlpullparserexception;import Com.itheima.pullparser.domain.city;import Android.os.bundle;import Android.app.activity;import android.util.Xml; Import Android.view.menu;import The android.view.view;//server transmits data to the client. This data is typically stored in an XML file. It is therefore necessary to parse the data, public class Mainactivity extends Activity {list<city> citylist; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {//Gets the resource file under the SRC folder Classloader.getresourceasstream. Returns the InputStream type. InputStream is = getClassLoader (). getResourceAsStream ("Weather.xml");//Get Pull Parser object Xmlpullparser is an interface, Cannot be newxmlpullparser XP = Xml.newpullparser ();//Initialize try {xp.setinput (IS, "utf-8");//Initialize, set file location stream object (parse target file object) and parse encoding format// Gets the event type of the current node, because pull parsing is parsed from the first wardrobe node of the XML file, the pointer moves down, and the label (node) is different and the event type is different. Judging by the event type, we can know the current sectionThe point is what node, thus determining what we should do operation int type = Xp.geteventtype (); City City = null;while (type! = xmlpullparser.end_document) {//xp. end_document//depending on the type of node, the switch (type) {case xmlpullparser.start_tag://gets the name of the current node if ("Weather") to do a different operation. Equals ( Xp.getname ())) {//Create a City collection object for the javabeancitylist of city = new arraylist<city> ();} else if ("City". Equals (Xp.getname ())) {//Create city JavaBean Object city = new City (); else if ("name". Equals (Xp.getname ())) {//Gets the next node of the current node [text]string name = Xp.nexttext (); City.setname (name);} else if ("temp". Equals (Xp.getname ())) {//Gets the text of the next node of the current node, string temp = Xp.nexttext (); city.settemp (temp);} else if ("PM". Equals (Xp.getname ())) {//Gets the text of the next node of the current node as string pm = Xp.nexttext (); CITY.SETPM (PM);} Break;case XmlPullParser.END_TAG:if ("City". Equals (Xp.getname ())) {//Put the city's JavaBean into the collection. Three cities, after parsing a city, put that city instance in the collection. Citylist.add (city);} break;} Move the pointer to the next node and return the node's event type type = Xp.next ();} for (city c:citylist) {System.out.println (c.tostring ());}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} 
Run the program and parse the result as follows:


Android Beginner Tutorial: Pull parsing in Android

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.