android之xmlpullparse解析器
Pull解析和Sax解析很相似,都是轻量级的解析,在Android的内核中已经嵌入了Pull,所以我们不需要再添加第三方jar包来支持Pull。Pull解析和Sax解析不一样的地方有(1)pull读取xml文件后触发相应的事件调用方法返回的是数字(2)pull可以在程序中控制想解析到哪里就可以停止解析。下面看我自己做的demo,来跟大家讲解下:
xml代码:
person.xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="20"> <name>zhangqian</name> <age>20</age> </person> <person id="23"> <name>zhangmeili</name> <age>23</age> </person> </persons>
<?xml version="1.0" encoding="UTF-8"?> 这是xml头文件 也叫做start document
<persons>这是xml节点(element) xml节点有两种类型(Node Type)1,element 2,text(就是上个element和下一个element之间称作文本类型)
通过对XML文件的大致特征,我们首先要建立一个javabean,建在一个domian包下,代码如下
package com.example.domian; public class Person { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } public Person(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Person(){} }
下面我们要开始处理业务层,对xml文件进行解析了,建在service包下,代码如下
package com.example.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import com.example.domian.Person; public class PersonService { public static List<Person> getPerson(InputStream xml) throws Exception { List<Person> persons =null; Person person = null; XmlPullParser pullParse = Xml.newPullParser(); pullParse.setInput(xml,"UTF-8"); int event = pullParse.getEventType(); while(event!=XmlPullParser.END_DOCUMENT){ switch (event) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG : if("person".equals(pullParse.getName())){ int id =new Integer(pullParse.getAttributeValue(0)); person = new Person(); person.setId(id); } if("name".equals(pullParse.getName())){ String name = pullParse.nextText(); person.setName(name); } if("age".equals(pullParse.getName())){ int age = new Integer(pullParse.nextText()); person.setAge(age); } break; case XmlPullParser.END_TAG : if("person".equals(pullParse.getName())){ persons.add(person); person = null; } break; } event = pullParse.next(); } return persons; } }
下面我们要对解析的方法是否正确,进行测试,写一个测试类,建立一个test包,代买如下
package com.example.test; import java.io.InputStream; import java.util.List; import com.example.domian.Person; import com.example.service.PersonService; import android.test.AndroidTestCase; import android.util.Log; public class TestPersonService extends AndroidTestCase { private static final String TAG = "TestPersonService"; public void testPerson() throws Exception{ InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons = PersonService.getPerson(xml); for(Person person:persons){ Log.i(TAG, person.toString()); } } }
要使用android的单元测试,需要在AndroidManifest.xml文件中加入声明,AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.xml" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.xml.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="android.test.runner"/> //主要配置这条 </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:label="Tests for My APP" android:targetPackage="com.example.xml"></instrumentation>
//主要配置这条
</manifest>
看JUnit 是否正确,绿色代表成功,红色代表失败
如图所示 成功!
看logcat日志,看运行结果!如图
很高兴和大家分享,如有什么问题及时和我交流,我会尽快回复。版权所有,如转载请注明出处http://www.cnblogs.com/jianrong-zheng/p/3548053.html谢谢!
posted on 2014-02-13 14:25 jianrong.zheng 阅读(1300) 评论(0) 编辑 收藏 举报