一,用pull解析xml
1,将要解析的文件放到src下面,因为程序打包后,会自动将src的文件一起打包的.
person.xml内容:
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>zhangsan</name> <age>30</age> </person> <person id="20"> <name>lisi</name> <age>25</age> </person> </persons>开始解析xml
/** * 获取数据 * * @param xml * @return * @throws Exception */ public static List<Person> getPersons(InputStream xml) throws Exception { List<Person> persons = null; Person person = null; XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(xml, "UTF-8");// 为Pull解析器设置要解析的XML数据,并且按照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); System.out.println("获取的id是:" + id); } if ("name".equals(pullParser.getName())) { String name = pullParser.nextText(); person.setName(name); System.out.println("获取的name是:" + name); } if ("age".equals(pullParser.getName())) { int age = new Integer(pullParser.nextText()); person.setAge(age); System.out.println("获取的age是:" + age); } break; case XmlPullParser.END_TAG:// 结束标签时 if ("person".equals(pullParser.getName())) { persons.add(person); person = null; } break; } event = pullParser.next(); } return persons; }测试:
public void testPersons() throws Exception { InputStream xml = this.getClass().getClassLoader().getResourceAsStream( "person.xml");//将src下的xml写入 List<Person> persons = PersonService.getPersons(xml); for (Person person : persons) { Log.i("解析xml的数据:", person.toString()); } }
二,生成xml文件:
生成的实体类
public static void save(List<Person> persons, OutputStream out) throws Exception { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true);//<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> serializer.startTag(null, "persons"); for (Person person : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "id", person.getId().toString()); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(person.getAge().toString()); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); out.flush(); out.close(); }
测试:
public void testSave() throws Exception { List<Person> persons = new ArrayList<Person>(); persons.add(new Person(43, "zhangsan", 80)); persons.add(new Person(12, "lisi", 20)); persons.add(new Person(78, "wangwu", 8)); // <包>/files File xmlFile = new File(getContext().getFilesDir(), "yjr.xml"); FileOutputStream outStream = new FileOutputStream(xmlFile); PersonService.save(persons, outStream); }生成的文件:
yjr.xml:将文件导出来
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <persons> <person id="43"> <name>zhangsan</name> <age>80</age> </person> <person id="12"> <name>lisi</name> <age>20</age> </person> <person id="78"> <name>wangwu</name> <age>8</age> </person> </persons>