android之xmlpullparse解析器

Pull解析和Sax解析很類似,都是輕量級的解析,在Android的內核中已經嵌入了Pull,因此咱們不須要再添加第三方jar包來支持Pull。Pull解析和Sax解析不同的地方有(1)pull讀取xml文件後觸發相應的事件調用方法返回的是數字(2)pull能夠在程序中控制想解析到哪裏就能夠中止解析。下面看我本身作的demo,來跟你們講解下:html

xml代碼:java

person.xmlandroid

<?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包下,代碼以下app

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包,代買以下dom

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以下:ide

<?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日誌,看運行結果!如圖this

很高興和你們分享,若有什麼問題及時和我交流,我會盡快回復。版權全部,如轉載請註明出處http://www.cnblogs.com/jianrong-zheng/p/3548053.html謝謝!spa

相關文章
相關標籤/搜索