Android-use the Pull parser to parse xml files

Source: Internet
Author: User

Two classes: one entity class Person. java, 1 class that inherits Activity 1. person. java [html] package com. mrzhu. work_shortsax; public class Person {private String personId; private String name; private String address; private String tel; private String fax; private String email; public String getPersonId () {return personId ;} public void setPersonId (String personId) {this. personId = personId;} public String getName () {return name ;} Public void setName (String name) {this. name = name;} public String getAddress () {return address;} public void setAddress (String address) {this. address = address;} public String getTel () {return tel;} public void setTel (String tel) {this. tel = tel;} public String getFax () {return fax;} public void setFax (String fax) {this. fax = fax;} public String getEmail () {return email;} public Void setEmail (String email) {this. email = email ;}@ Override public String toString () {return "Person [personId =" + personId + ", name =" + name + ", address = "+ address +", tel = "+ tel +", fax = "+ fax +", email = "+ email +"] ";}} 2. class inheriting Activity: [html] package com. mrzhu. work_‑sax; import java. io. IOException; import java. io. inputStream; import java. util. arrayList; import java. util. list; imp Ort javax. xml. parsers. parserConfigurationException; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. SAXException; import org. xmlpull. v1.XmlPullParser; import org. xmlpull. v1.XmlPullParserException; import android. app. activity; import android. OS. bundle; import android. util. xml; import android. view. view; import android. widget. toast; public class PullA Ctivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);} public void pull (View v) {pullParse ();} public void pullParse () {List <Person> persons = null; Person person = null; inputStream in = null; try {XmlPullParser pullParser = Xml. newPullParser (); In = getAssets (). open ("person. xml"); pullParser. setInput (in, "UTF-8"); int eventType = pullParser. getEventType (); while (eventType! = XmlPullParser. END_DOCUMENT) {String qName = pullParser. getName (); switch (eventType) {case XmlPullParser. START_DOCUMENT: persons = new ArrayList <Person> (); break; case XmlPullParser. START_TAG: if ("person ". equalsIgnoreCase (qName) {person = new Person (); person. setPersonId (pullParser. getAttributeValue (null, "personid");} else if ("name ". equalsIgnoreCase (qName) {person. setName (pullParser. nextText () );} Else if ("address ". equalsIgnoreCase (qName) {person. setAddress (pullParser. nextText ();} else if ("tel ". equalsIgnoreCase (qName) {person. setTel (pullParser. nextText ();} else if ("fax ". equalsIgnoreCase (qName) {person. setFax (pullParser. nextText ();} else if ("email ". equalsIgnoreCase (qName) {person. setEmail (pullParser. nextText ();} break; case XmlPullParser. END_TAG: if ("person ". equalsIgnoreCase (qNa Me) {persons. add (person);} break;} eventType = pullParser. next ();} in. close ();} catch (XmlPullParserException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} for (Person person2: persons) {Toast. makeText (this, person2.toString (), Toast. LENGTH_SHORT ). show () ;}} main. xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <Button android: layout_width = "match_parent" android: layout_height = "wrap_content" android: onClick = "pull" android: text = "pull"/> </LinearLayout> person placed in the assets folder. xml file [html] <? Xml version = "1.0" encoding = "UTF-8"?> <People> <person personid = "E01"> <name> Tony Blair </name> <address> 10 Downing Street, London, UK </address> <tel> (061) 98765 </tel> <fax> (061) 98765 </fax> <email> blair@everywhere.com </email> </person> <person personid = "E02"> <name> Bill Clinton </name> <address> White House, USA </address> <tel> (001) 6400 98765 </tel> <fax> (001) 6400 98765 </fax> <email> bill@everywhere.com </email> </person> <person personid = "E03"> <name> Tom Cruise </name> <address> 57 Jumbo street, new York, USA </address> <tel> (001) 4500 67859 </tel> <fax> (001) 4500 67859 </fax> <email> cruise@everywhere.com </email> </person> www.2cto.com <person personid = "E04"> <name> Linda Goodman </name> <address> 78 crax Lane, london, UK </address> <tel> (061) 54 56789 </tel> <fax> (061) 54 56789 </fax> <email> linda@everywhere.com </email> </person> </people>

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.