创建实体bean类:
package com.example.xmltest.entity; public class Person { private Integer id; private String name; private Integer age; public Person() { super(); } public Person(Integer id,String name,Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ",name=" + name + ",age=" + age + "]"; } }
序列化和解析的方法:
package com.example.xmltest.utils; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Log; import android.util.Xml; import com.example.xmltest.entity.Person; public class XmlUtils { private static final String TAG = "XmlUtils"; /** * * 将数据以xml形式写入到本地 * * @throws Exception */ public void writeToLocal() throws Exception { List<Person> personList = new ArrayList<Person>(); for (int i = 0; i < 30; i++) { personList.add(new Person(i,"王" + i,20 + i)); } XmlSerializer serializer = Xml.newSerializer();// 创建 XmlSerializer对象 FileOutputStream fos = new FileOutputStream("/mnt/sdcard/person.xml"); // 指定序列化对象输出的流 serializer.setOutput(fos,"utf-8"); // 写xml的头 serializer.startDocument("utf-8",true);// true为yes-----> xml文件中无DTD约束。 // 开始的根节点 serializer.startTag(null,"persons"); for (Person person : personList) { serializer.startTag(null,"person"); serializer.attribute(null,"id",String.valueOf(person.getId())); // 写人的姓名 serializer.startTag(null,"name"); serializer.text(person.getName()); serializer.endTag(null,"name"); // 写人的年龄 serializer.startTag(null,"age"); serializer.text(String.valueOf(person.getAge())); serializer.endTag(null,"age"); serializer.endTag(null,"person"); } serializer.endTag(null,"persons");// 不用写“/” // 标记xml文件输出完毕 serializer.endDocument(); } /** * * 从本地解析xml数据 * * @throws Exception * * */ public void parserXmlFromLocal() throws Exception { // 创建pull解析器对象 XmlPullParser parser = Xml.newPullParser(); // 指定解析的文件 FileInputStream fis = new FileInputStream("/mnt/sdcard/person.xml"); parser.setInput(fis,"UTF-8"); int eventType = parser.getEventType();// 获得解析器的第一个事件类型 List<Person> personList = null; Person person = null; String name; String tagName = parser.getName(); while (eventType != XmlPullParser.END_DOCUMENT) {// 如果事件类型不等于结束的类型,继续循环 // 解析数据 switch (eventType) { case XmlPullParser.START_TAG:// 代表开始节点<persons> if ("persons".equals(tagName)) { // 初始化集合 personList = new ArrayList<Person>(); } else if ("person".equals(tagName)) { person = new Person(); person.setId(Integer.valueOf(parser.getAttributeValue(null,"id"))); } else if ("name".equals(tagName)) { name = parser.nextText(); // 当前是<name>节点 // nextText是取<name>后边的值 person.setName(name); } else if ("age".equals(tagName)) { person.setAge(Integer.parseInt(parser.nextText())); } break; case XmlPullParser.END_TAG: // 代表结束节点</person> if ("person".equals(tagName)) {// 当前是</person> // 把上面person的对象添加到对象中 personList.add(person); } break; default: break; } eventType = parser.next();// 赋值下一个事件类型 } if (personList != null) { for (Person p : personList) { Log.i(TAG,p.toString()); System.out.println(p.toString()); } } } }
测试的方法:
package com.example.xmltest.utils; import android.test.AndroidTestCase; public class TestCase extends AndroidTestCase { public void test() throws Exception{ XmlUtils xmlUtils = new XmlUtils(); xmlUtils.writeToLocal(); xmlUtils.parserXmlFromLocal(); } }
因为用到测试方法,所以需要引入测试框架,另外需要假如写sd卡的权限。