Android使用Pull解析器解析xml文件的实现代码
2个类:1个实体类person.java,1个继承activity的类
1.person.java
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.继承activity的类:
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 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(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
<?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>
放在assets文件夹下的person.xml文件
<?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) 54 56789</tel>
<fax>(061) 54 56789</fax>
<email>linda@everywhere.com</email>
</person>
</people>
上一篇: 迅雷快鸟CPU占用过高现象的解决办法介绍
下一篇: Android 开机广播的使用及配置