Android--使用Pull解析器解析xml文件
2个类:1个实体类Person.java,1个继承Activity的类1.Person.java
[html]
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的类:
[html]
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)){
 
补充:移动开发 , Android ,