Android使用Pull解析器解析xml檔案的實現代碼

來源:互聯網
上載者:User

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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.