<?xml version="1.0" encoding="UTF-8"?><persons>
<person id="1">
<name>张三</name>
<age>18</age>
</person>
<person id="2">
<name>李四</name>
<age>14</age>
</person>
</persons>
1、首先是准备xml文件
2、进行解析
public class XmlService {
/**
* 获取Person
* document 解析:把整个文档树读入到内存中,内存消耗大
* sax解析 :基于事件,一个一个的解析
*
* android有自己的xml解析机制:Pull解析 --》和sax解析很像,也是基于事件的
*
*
*
* @return
* @throws Exception
*/
public List<PersonInfo> getPersons(InputStream inputStream, String inputEncoding) throws Exception{
List<PersonInfo> infos = null;
PersonInfo info = null;
//解析工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//通过解析工厂解析器
XmlPullParser parser = factory.newPullParser();
//设置解析数据
parser.setInput(inputStream, inputEncoding);
//获取解析事件类型
int eventType = parser.getEventType();
//只要没有解析到文档的结尾就继续解析
while(eventType != XmlPullParser.END_DOCUMENT){
switch (eventType) {
//parser.getName() 获取节点的名字
case XmlPullParser.START_TAG:
if("persons".equals(parser.getName())){
infos = new ArrayList<PersonInfo>();
}else if("person".equals(parser.getName())){
info = new PersonInfo();
//parser.getAttributeValue(0) 获取节点的属性
int id = Integer.parseInt(parser.getAttributeValue(0));
info.id = id;
}else if("name".equals(parser.getName())){
//parser.nextText() 下个文本节点的值
String name = parser.nextText();
info.name = name;
}else if("age".equals(parser.getName())){
int age = Integer.parseInt(parser.nextText());
info.age = age;
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
infos.add(info);
info = null;
}
break;
default:
break;
}
//继续解析
eventType = parser.next();
}
return infos;
}
xml文件的生成
/**
* 把对象集合以xml的形式保存
* @param infos
* @throws Exception
*/
public void createXml(List<PersonInfo> infos,OutputStream os) throws Exception{
/*XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//序列化器
XmlSerializer serializer = factory.newSerializer();*/
XmlSerializer serializer = Xml.newSerializer(); //xml封装好的序列化器对象方法,相当于上面两行代码
//指定输出流
serializer.setOutput(os, "UTF-8");
//文档开始
serializer.startDocument("UTF-8", true);
//标签开始
serializer.startTag(null, "persons");
for(PersonInfo info:infos){
serializer.startTag(null, "person");
//属性
serializer.attribute(null, "id", info.id+"");
//name
serializer.startTag(null, "name");
serializer.text(info.name);
serializer.endTag(null, "name");
//age
serializer.startTag(null, "age");
serializer.text(info.age+"");
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
//标签的结束
serializer.endTag(null, "persons");
//文档结束
serializer.endDocument();
}
准备数据类接收
</pre><pre class="java" name="code">package com.jky.xml.domain;
public class PersonInfo {
public int id;
public String name;
public int age;
public PersonInfo() {
super();
// TODO Auto-generated constructor stub
}
public PersonInfo(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "PersonInfo [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
测试类进行测试
public class XmlServiceTest extends AndroidTestCase {
public void testGetPersons() throws Exception{
XmlService xs = new XmlService();
//获取流,此时模拟的xml文件需要在src文件夹下
// InputStream inputStream = getClass().getClassLoader().getResourceAsStream("persons.xml");
//此时模拟的xml文件可以在当前测试类文件夹下
InputStream inputStream = getClass().getResourceAsStream("persons.xml");
List<PersonInfo> persons = xs.getPersons(inputStream, "UTF-8");
for(PersonInfo info:persons){
Log.i("i", info.toString());
}
}
public void testCreateXml() throws Exception{
XmlService xs = new XmlService();
List<PersonInfo> infos = new ArrayList<PersonInfo>();
infos.add(new PersonInfo(1, "james", 18));
infos.add(new PersonInfo(2, "allen", 21));
OutputStream os = getContext().openFileOutput("persons.xml", Context.MODE_PRIVATE);
xs.createXml(infos, os);
}
}