android xml pull方法二

32568 ワード

引用:http://www.cnblogs.com/xpxpxp2046/archive/2011/12/18/2291946.html
Pull解析とSax解析は似ています.全部ライト級の解析です.Androidのカーネルに既にPullが埋め込まれていますので、第三者jarパッケージを追加してPullをサポートする必要はありません.
Pull解析とSax解析の違いがあります.
1)PullがXMLファイルを読み込み、対応するイベント起動方法をトリガして戻ってきたのは数字です.
2)Pullはプログラムの中でコントロールできます.解析はどこで行いますか?
実例を見る:
book s.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<name>thinking in java</name>
<price>85.5</price>
</book>
<book id="2">
<name>Spring in Action</name>
<price>39.0</price>
</book>
</books>
Pulservice.java
package cn.powerise.xmlparse.demo.service;
import
java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import cn.powerise.xmlparse.demo.entity.Book;

public class PullParseService {
public static List<Book> getBooks(InputStream inputStream) throws Exception{
List<Book> books = null;
Book book = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "UTF-8");

int event = parser.getEventType();//
while(event!=XmlPullParser.END_DOCUMENT){
switch(event){
case XmlPullParser.START_DOCUMENT://
books = new ArrayList<Book>();// books
break;
case XmlPullParser.START_TAG://
if("book".equals(parser.getName())){// book
book = new Book();
book.setId(Integer.parseInt(parser.getAttributeValue(0)));// book , book id
}
if(book!=null){
if("name".equals(parser.getName())){// name
book.setName(parser.nextText());
}else if("price".equals(parser.getName())){// price
book.setPrice(Float.parseFloat(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG://
if("book".equals(parser.getName())){// book
books.add(book);// book books
book = null;
}
break;
}
event = parser.next();//
}//end while
return books;
}
}
Book.java
 
   
package com.powerise.xmlparse.demo.entity;
public class Book {
    private int id;
private String name;
private float price;
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;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString(){
return this.id+":"+this.name+":"+this.price;
}
}
以下はAndroidのユニットを使ってテストします.コードは以下の通りです.
package cn.powerise.test;
import
java.io.InputStream;
import java.util.List;

import android.test.AndroidTestCase;
import android.util.Log;

import com.powerise.xmlparse.demo.entity.Book;
import com.powerise.xmlparse.demo.service.PullParseService;

public class TestPullParseService extends AndroidTestCase{
private static final String TAG = "TestPullParseService";
public void testPull() throws Exception{
InputStream input = this.getClass().getClassLoader().getResourceAsStream("book.xml");
PullParseService pull = new PullParseService();
List<Book> books = pull.getBooks(input);
for(Book book : books){
Log.i(TAG,book.toString());
}
}
}
Android Manifest.xmlは以下の通りです.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="cn.powerise.xmlparse.demo"
android:versionCode
="1"
android:versionName
="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
<activity android:name=".PullParseDemo"
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>
<uses-sdk android:minSdkVersion="7" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage
="cn.powerise.xmlparse.demo" android:label="Tests for My App" />
</manifest>