使用Pull解析器读取XML文件和生成XML文件

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 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包下添加类:

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());
}


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值