首页>>程序代码>>使用Pull解析器读取XML文件和生成XML文件
使用Pull解析器读取XML文件和生成XML文件
来源: http://secyaher.blog.163.com/ 时间: 2014-12-22 21:05:49

除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android内置的Pull解析器解析XML文件。 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。

Pull解析器的源码及文档下载网址:http://www.xmlpull.org/

Pull解析技术是第三方开发的开源技术,它同样可以应用于JavaSE开发。

在《使用SAX读取XML文件》文章中的com.jbridge.xml.service包下添加类:

package com.jbridge.xml.service

import java.io.InputStream;

import java.io.OutputStream;

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 com.jbridge.xml.domain.Person;

import android.util.Xml;

public class PullPersonService {

public static List<Person> getPersons(InputStream inStream)

throws Exception {

List<Person> persons = null;

Person person = null;

// 创建Pull解析

// xmlPullParserFactory=XmlPullParserFactory.newInstance();

// XmlPullParser pullParser =xmlPullParserFactory.newPullParser();

XmlPullParser pullParser = Xml.newPullParser();// 这行代码同上面两行

// 解析XML

pullParser.setInput(inStream, "UTF-8");

// 开始

int event = pullParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {

switch (event) {

// 文档开始

case XmlPullParser.START_DOCUMENT:

persons = new ArrayList<Person>();

break;

// 节点开始

case XmlPullParser.START_TAG:

if ("person".equals(pullParser.getName())) {

int id = new Integer(pullParser.getAttributeValue(0));

person = new Person();

person.setId(id);

}

if (person != null) {

if ("name".equals(pullParser.getName())) {

String name = pullParser.nextText();// 如果后面是Text元素,即返回它的值

person.setName(name);

} else if ("age".equals(pullParser.getName())) {

String age = pullParser.nextText();

person.setAge(new Short(age));

}

}

break;

// 节点结束

case XmlPullParser.END_TAG:

if ("person".equals(pullParser.getName())) {

persons.add(person);

person = null;

}

break;

}

event = pullParser.next();

}

return persons;

}

public static void save(List<Person> persons, OutputStream outStream) throws Exception{

   XmlSerializer serializer = Xml.newSerializer();

   serializer.setOutput(outStream, "UTF-8");

   serializer.startDocument("UTF-8", true);

   serializer.startTag(null, "persons");// serializer.startTag("", "persons");

   for(Person person : persons){

     serializer.startTag(null, "person");// serializer.startTag("", "person");

     serializer.attribute(null, "id", person.getId().toString()); // serializer.attribute("", "id", person.getId().toString());

     serializer.startTag(null, "name");  //serializer.startTag("", "name");

     serializer.text(person.getName());

     serializer.endTag(null, "name");// serializer.endTag("", "name");

     serializer.startTag(null, "age"); //serializer.startTag("", "age");

     serializer.text(person.getAge().toString());

     serializer.endTag(null, "age"); // serializer.endTag("", "age");

     serializer.endTag(null, "person");//serializer.endTag("", "person");

   }

   serializer.endTag(null, "persons");    //serializer.endTag("", "persons");

   serializer.endDocument();

   outStream.flush();

   outStream.close();

}

public static void save(List<Person> persons,Writer write) throws Exception{

   XmlSerializer serializer = Xml.newSerializer();

   serializer.setOutput(write);

   serializer.startDocument("UTF-8", true);

   serializer.startTag(null, "persons");// serializer.startTag("", "persons");

   for(Person person : persons){

     serializer.startTag(null, "person");// serializer.startTag("", "person");

     serializer.attribute(null, "id", person.getId().toString()); // serializer.attribute("", "id", person.getId().toString());

     serializer.startTag(null, "name");  //serializer.startTag("", "name");

     serializer.text(person.getName());

     serializer.endTag(null, "name");// serializer.endTag("", "name");

     serializer.startTag(null, "age"); //serializer.startTag("", "age");

     serializer.text(person.getAge().toString());

     serializer.endTag(null, "age"); // serializer.endTag("", "age");

     serializer.endTag(null, "person");//serializer.endTag("", "person");

   }

   serializer.endTag(null, "persons");    //serializer.endTag("", "persons");

   serializer.endDocument();

   write.flush();

   write.close();

}

}

在《使用SAX读取XML文件》文章中的TestService类下添加方法:

public void testPullGetPersons() throws Throwable{

InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("person.xml");

PullPersonService pullPersonService=new PullPersonService();

List<Person> persons = pullPersonService.getPersons(inStream);

for(Person person : persons){

Log.i(TAG, person.toString());

}

}

public void testPullSave() throws Throwable{

// List<Person> persons = new ArrayList<Person>();

// persons.add(new Person(34, "xiaozhang", (short)30));

// persons.add(new Person(56, "xiaozhu", (short)12));

// persons.add(new Person(89, "xiaoli", (short)50));

// File file = new File(this.getContext().getFilesDir(), "zyj.xml");//  /data/data/com.jbridge.xml.activity/files/zyj.xml

// FileOutputStream outStream = new FileOutputStream(file);

// BufferedOutputStream b=new BufferedOutputStream(outStream);

// OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");

// BufferedWriter writer = new BufferedWriter(outStreamWriter);

// PullPersonService.save(persons, b);

// List<Person> persons = new ArrayList<Person>();

// persons.add(new Person(34, "xiaozhang", (short)30));

// persons.add(new Person(56, "xiaozhu", (short)12));

// persons.add(new Person(89, "xiaoli", (short)50));

// File file = new File(this.getContext().getFilesDir(), "zyj.xml");

// FileOutputStream outStream = new FileOutputStream(file);

// OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");

// BufferedWriter writer = new BufferedWriter(outStreamWriter);

// PullPersonService.save(persons, writer);

// 生成到内存中。

List<Person> persons = new ArrayList<Person>();

persons.add(new Person(34, "xiaozhang", (short)30));

persons.add(new Person(56, "xiaozhu", (short)12));

persons.add(new Person(89, "xiaoli", (short)50));

StringWriter sw=new StringWriter();

PullPersonService.save(persons,sw);

Log.i(TAG, sw.toString());

}

本页地址:http://www.easytd.com/chengxudaima/d_14122221528.html

上一篇:Java Pattern和Matcher正则表达式使用总结 下一篇:Spring和Hibernate框架操作数据库水平分区