昨天照着视频上的例子练习了下android自带的pull解析器来解析XML
新建一个工程,在SRC目录下建了了个person.xml用来被解析:
<?xml version="1.0" encoding="utf-8"?>
<persons>
<person id ="11">
<name>tom</name>
<age>30</age>
</person>
<person id ="13">
<name>jame</name>
<age>25</age>
</person >
</persons>
xml文件要注意格式,写错了的话会导致程序报错的,我就是因为id = 11这个没有加引号导致一直报错;
建一个Person对象,获得解析的数据Person.xml
public class Person {
private int age,id;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
}
}
再建一个解析类ParseXML.java
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class ParseXML {
//解析方法,返回一个List容器,存储Person对象
public static List<Person> getPersons (InputStream xmlStream) throws Exception{
List<Person> persons = null;
Person person = null;
//新建一个解析器
XmlPullParser xmlPullParser = Xml.newPullParser();
xmlPullParser.setInput(xmlStream, "UTF-8");
//获得解析状态,解析状态为int值
int event = xmlPullParser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(xmlPullParser.getName())){
int id =new Integer(xmlPullParser.getAttributeValue(0));
person = new Person();
person.setId(id);
}
if("name".equals(xmlPullParser.getName())){
String name = xmlPullParser.nextText();
person.setName(name);
}
if("age".equals(xmlPullParser.getName())){
int age = new Integer(xmlPullParser.nextText());
person.setAge(age);
}
break;
case XmlPullParser.END_TAG:
if("person".equals(xmlPullParser.getName())) {
persons.add(person);
}
break;
}
event = xmlPullParser.next();
}
return persons;
}
}
写一个测试模块,对解析方法进行测试,需要在AndroidManifest.xml进行声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.study.XMLTest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- 添加uses-library, 注意要添加在application标签里面 -->
<uses-library android:name="android.test.runner"/>
<activity android:name=".XMLTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 添加instrumentation -->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.study.XMLTest" android:label="test my app"></instrumentation>
</manifest>
新建一个测试类Test.java,需要继承AndroidTestCase
public class Test extends AndroidTestCase {
public void testPersons() throws Exception{
InputStream teStream=this.getClass().getClassLoader().getResourceAsStream("person.xml");
List<Person> persons = ParseXML.getPersons(teStream);
for(Person person:persons){
System.out.println(person.toString());
}
}
}
右键点击工程Run as Android JUnit Test 可得到打印结果:
10-15 20:16:08.285: INFO/System.out(13535): Person [age=30, id=11, name=tom]
10-15 20:16:08.285: INFO/System.out(13535): Person [age=25, id=13, name=jame]