Android Pull 解析 XML

Pull 解析和 Sax 解析很相似,都是轻量级的解析。在 Android 的内核中已经嵌入了 Pull,所以我们不需要再添加第三方 jar 包来支持 Pull。

Pull 解析和 Sax 解析不一样的地方有:

1)Pull 读取 XML 文件后触发相应的事件调用方法返回的是数字

2)Pull 可以在程序中控制想解析到哪里就可以停止解析

<?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>

 

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;
    }
}
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 单元测试类
  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());
            }
        }
    }

 

androidmainfest.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>

你可能感兴趣的:(Android Pull 解析 XML)