首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Android(3)数据存储之XML解析技术 2

2012-08-31 
Android(三)数据存储之XML解析技术 2二、Pull解析技术?????? Pull解析技术与Sax解析技术原理相同,但比Sax解

Android(三)数据存储之XML解析技术 2

二、Pull解析技术

?????? Pull解析技术与Sax解析技术原理相同,但比Sax解析简单,它们的解析速度和占用的资源差不多。Android内部使用的XML解析技术正是Pull,Android官方推荐开发者们使用Pull解析技术。Pull解析技术是第三方开发的开源技术,它同样可以应用于JavaSE开发。下面我们使用Pull解析技术解析XML文件,然后再使用Pull技术生成XML文件。

?

Pull解析XML文档

?????? 1.XML文件

?????? 依然使用上面的books.xml

?

?????? 2.XML文档对应的实体Book

?????? 依然使用上面的Book.java

??????

?????? 3.Pull解析XML类

package com.changcheng.androidxml.xml;

?

import java.io.InputStream;

import java.io.Writer;

import java.util.ArrayList;

import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserFactory;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

import com.changcheng.androidxml.entity.Book;

?

public class AndroidPullXML {

?

???????? public static List<Book> readXML(InputStream inputStream,

??????????????????????????? String inputEncoding) throws Exception {

?????????????????? // 创建Pull解析

?????????????????? XmlPullParserFactory pullParserFactory = XmlPullParserFactory

???????????????????????????????????? .newInstance();

?????????????????? XmlPullParser pullParser = pullParserFactory.newPullParser();

?????????????????? // 解析XML

?????????????????? pullParser.setInput(inputStream, inputEncoding);

?????????????????? // 开始

?????????????????? int eventType = pullParser.getEventType();

??????????????????

?????????????????? List<Book> books = null;

?????????????????? Book book = null;

?????????????????? while (eventType != XmlPullParser.END_DOCUMENT) {

??????????????????????????? String nodeName = pullParser.getName();

??????????????????????????? switch (eventType) {

??????????????????????????? // 文档开始

??????????????????????????? case XmlPullParser.START_DOCUMENT:

???????????????????????????????????? books = new ArrayList<Book>();

???????????????????????????????????? break;

??????????????????????????? // 节点开始

??????????????????????????? case XmlPullParser.START_TAG:

???????????????????????????????????? if ("book".equals(nodeName)) {

?????????????????????????????????????????????? book = new Book();

?????????????????????????????????????????????? book.setId(Integer

???????????????????????????????????????????????????????????????? .parseInt(pullParser.getAttributeValue(0)));

???????????????????????????????????? } else if ("name".equals(nodeName)) {

?????????????????????????????????????????????? book.setName(pullParser.nextText());

???????????????????????????????????? } else if ("price".equals(nodeName)) {

?????????????????????????????????????????????? book.setPrice(Float.parseFloat(pullParser.nextText()));

???????????????????????????????????? }

???????????????????????????????????? break;

??????????????????????????? // 节点结束

??????????????????????????? case XmlPullParser.END_TAG:

???????????????????????????????????? if ("book".equals(nodeName)) {

?????????????????????????????????????????????? books.add(book);

?????????????????????????????????????????????? book = null;

???????????????????????????????????? }

???????????????????????????????????? break;

??????????????????????????? }

??????????????????????????? eventType = pullParser.next();

?????????????????? }

?????????????????? return books;

???????? }

}

?

?????? 4.编写测试Pull解析XML类

?????? 在sax测试类TestAndroidXML中添加一个测试方法:

/**

?* 测试Pull解析XML

?* @throws Throwable

?*/

public void testAndroidPullReadXML() throws Throwable {

???????? InputStream file = this.getClass().getClassLoader().getResourceAsStream("books.xml");

???????? try {

?????????????????? List<Book> books = AndroidPullXML.readXML(file, "UTF-8");

?????????????????? Log.i(TAG, books.toString());

???????? } catch (Exception e) {

?????????????????? Log.e(TAG, e.toString());

???????? }

}

?

?????? 5.运行测试

?????? 在outline面板中的testAndroidPullReadXML方法或在TestAndroidXML类的testAndroidPullReadXML方法上右键->Debug As->Android Junit Test。运行结束后在LogCat面板中查看运行结束。

?

Pull生成XML文档

?????? 使用Pull生成上面的books.xml文档。

??????

?????? 1.在AndroidPullXML类中添加一个方法:

public static void writeXML(Writer writer, List<Book> books)

?????????????????? throws Exception {

???????? // 创建XML生成器

???????? XmlSerializer writexml = Xml.newSerializer();

???????? writexml.setOutput(writer);

???????? // 生成XML文档

???????? writexml.startDocument("UTF-8", true);

???????? writexml.startTag("", "books");

???????? for (Book book : books) {

?????????????????? // name

?????????????????? writexml.startTag("", "name");

?????????????????? writexml.attribute("", "id", book.getId() + "");

?????????????????? writexml.text(book.getName());

?????????????????? writexml.endTag("", "name");

?????????????????? // price

?????????????????? writexml.startTag("", "price");

?????????????????? writexml.text(book.getPrice() + "");

?????????????????? writexml.endTag("", "price");

???????? }

???????? //

???????? writexml.endTag("", "books");

}

?

?????? 2.编写测试Pull生成XML方法

?????? 在sax测试类TestAndroidXML中添加一个测试方法:

/**

?* 测试Pull生成XML

?* @throws Throwable

?*/

public void testAndroidPullWriteXML() throws Throwable {

???????? // 生成到内存中。(也可以生成到文件中,那就需要定义一个文件输出流。)

???????? StringWriter writer = new StringWriter();

???????? // 添加三本书

???????? List<Book> books = new ArrayList<Book>();

???????? books.add(new Book(1, "C", 89));

???????? books.add(new Book(1, "C++", 100));

???????? books.add(new Book(1, "Java", 87));

???????? books.add(new Book(1, "JavaEE", 95));

???????? // 生成XML

???????? AndroidPullXML.writeXML(writer, books);

???????? // 打印结果

???????? Log.i(TAG, books.toString());

?

}

?

?????? 3.运行测试

?????? 在outline面板中的testAndroidPullWriteXML方法或在TestAndroidXML类的testAndroidPullWriteXML方法上右键->Debug As->Android Junit Test。运行结束后在LogCat面板中查看运行结束。

?

?????? OK,使用Sax和Pull在Android中解析XML文档到此完成。在Andorid中还可以使用DOM技术,使用DOM技术解析在我们学习JavaWeb基础时,已经做了总结,在此就不再介绍了。

?

热点排行