Android uses pull parser to parse XML file implementation code _android

Source: Internet
Author: User

2 classes: 1 entity classes person.java,1 A class that inherits the activity

1.person.java

Copy Code code as follows:

Package com.mrzhu.work_1_sax;

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. Classes inheriting the activity:

Copy Code code as follows:

Package com.mrzhu.work_1_sax;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;

Import 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 Pullactivity extends activity {
/** called the activity is a. */
@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 (QName)) {
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

Copy Code code as follows:

<?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.xml files placed under the Assets folder

Copy Code code as follows:

<?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>
<person personid= "E04" >
<name>linda goodman</name>
<address>78 Crax Lane, London, uk</address>
<tel> (061) 56789</tel>
<fax> (061) 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.