pull方式保存和读取xml
1、pull相关介绍
pull解析方式和sax类似都是采用事件驱动模式,区别在sax是方法回调,pull是返回数字。因此可以使用一个switch对感兴趣的事件进行处理。
2、代码说明
PullPersonService
package com.tjp.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.XmlSerializer;
import android.util.Xml;
import com.tjp.model.Person;
/**
*
* 使用pull技术解析
* @author TanJianping
*
*/
public class PullPersonService {
public static List<Person> getPersons(InputStream inputStream)throws Exception{
List<Person> persons=null;
Person person=null;
XmlPullParser pullParser=Xml.newPullParser();//获得pull解析对象
pullParser.setInput(inputStream, "UTF-8");//设置文件流
int eventType=pullParser.getEventType();//获得第一个事件
while(eventType!=XmlPullParser.END_DOCUMENT){//如果事件不为文档解析结束
switch (eventType) {
case XmlPullParser.START_DOCUMENT: //开始解析文档
persons=new ArrayList<Person>();
break;
case XmlPullParser.START_TAG: //开始解析元素
String name=pullParser.getName();//获得解析器当前指向的元素名称
if(name.equals("person")){
person=new Person();
person.setId(Integer.parseInt(pullParser.getAttributeValue(0)));//获得解析器当前指向的元素的属性,根据索引
}
if(person!=null){
if(name.equals("age")){
person.setAge(Integer.parseInt(pullParser.nextText()));//获得解析器当前指向的当前节点的下一个节点的文本值
}else if(name.equals("name")){
person.setName(pullParser.nextText());
}
}
break;
case XmlPullParser.END_TAG://解析元素结束
if(pullParser.getName().equals("person")){
persons.add(person);
person=null;
}
break;
}
eventType=pullParser.next();//解析的下一个节点
}
return persons;
}
/**
* 保存xml
* @param persons
* @param outputStream
* @throws Exception
*/
public static void saveFile(List<Person> persons,OutputStream outputStream)throws Exception{
XmlSerializer serializer=Xml.newSerializer();//获得pull序列话对象
serializer.setOutput(outputStream, "UTF-8");//设置xml头部
serializer.startDocument("UTF-8", true);//文档开始
serializer.startTag(null, "persons");//设置一个节点
for(Person person :persons){
serializer.startTag(null, "person");
serializer.attribute(null, "id", String.valueOf(person.getId()));//设置节点的属性值
serializer.startTag(null, "age");
serializer.text(String.valueOf(person.getAge()));//设置文本值
serializer.endTag(null, "age");
serializer.startTag(null, "name");
serializer.text(String.valueOf(person.getName()));
serializer.endTag(null, "name");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");//结束一个节点
serializer.endDocument();//文档结束
outputStream.flush();
outputStream.close();
}
/**
* 保存xml用写入器的方式
* @param persons
* @param outputStream
* @throws Exception
*/
public static void saveFile4Writer(List<Person> persons,Writer writer)throws Exception{
XmlSerializer serializer=Xml.newSerializer();//获得pull序列话对象
//serializer.setOutput(writer, "UTF-8");//设置xml头部
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);//文档开始
serializer.startTag(null, "persons");//设置一个节点
for(Person person :persons){
serializer.startTag(null, "person");
serializer.attribute(null, "id", String.valueOf(person.getId()));//设置节点的属性值
serializer.startTag(null, "age");
serializer.text(String.valueOf(person.getAge()));//设置文本值
serializer.endTag(null, "age");
serializer.startTag(null, "name");
serializer.text(String.valueOf(person.getName()));
serializer.endTag(null, "name");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");//结束一个节点
serializer.endDocument();//文档结束
writer.flush();
writer.close();
}
}
PullTest
public void testSavePerson4Pull4Writer()throws Exception{
//OutputStream outputStream=this.getContext().openFileOutput("persons.xml", Context.MODE_PRIVATE);
//OutputStreamWriter writer=new OutputStreamWriter(outputStream);
//BufferedWriter bufferedWriter=new BufferedWriter(writer);//用缓冲区
StringWriter stringWriter=new StringWriter();//写入内存
List<Person> persons=new ArrayList<Person>();
persons.add(new Person(11,"谭建平1",12));
persons.add(new Person(12,"谭建平2",22));
persons.add(new Person(13,"谭建平3",32));
PullPersonService.saveFile4Writer(persons, stringWriter);
Log.i(TAG, stringWriter.toString());//从内存中读取
}