xml文件:assets/person.xml
<?xml version="1.0" encoding="utf-8"?> <persons> <person id="1"> <name>jack</name> <password>12345</password> </person> <person id="2"> <name>tom</name> <password>45733</password> </person> <person id="3"> <name>helen</name> <password>123s432</password> </person> </persons>
person类:
package com.example.xmldemo.domain; public class Person { private int id; private String name; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Person(int id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } public Person(String name, String password) { super(); this.name = name; this.password = password; } public Person() { } }
把xml文件内容转成object list:
private List<Person> xmlToObjList() { XmlPullParser parser = Xml.newPullParser(); InputStream is = null; List<Person> xmlDatas = new ArrayList<Person>(); try { is = getAssets().open("person.xml"); // 关联要解析的xml文档 parser.setInput(is, "utf-8"); Person person = null; int event = parser.getEventType(); // 不等于文档结束事件循环 while (event != XmlPullParser.END_DOCUMENT) { if (XmlPullParser.START_TAG == event) { // person标签开始 if ("person".equals(parser.getName())) { person = new Person(); int pid = Integer.parseInt(parser.getAttributeValue(0)); person.setId(pid); } // name标签开始 if ("name".equals(parser.getName())) { String name = parser.nextText(); person.setName(name); } if ("password".equals(parser.getName())) { String password = parser.nextText(); person.setPassword(password); } } if (XmlPullParser.END_TAG == event) { // person标签结束 if ("person".equals(parser.getName())) { xmlDatas.add(person); } } event = parser.next(); } // 验证: for (Person p : xmlDatas) { Log.i("XML", p.getName() + p.getPassword()); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return xmlDatas; }
把object list内容写入xml:
private void objToXml(List<Person> xmlDatas) { XmlSerializer serializer = Xml.newSerializer(); OutputStream os = null; try { // 保存位置 os = openFileOutput("person.xml", MODE_PRIVATE); serializer.setOutput(os, "utf-8"); // 生成xml文件 serializer.startDocument("utf-8", false); // 开始标签:<persons> serializer.startTag(null, "persons"); for (Person person : xmlDatas) { // <person> serializer.startTag(null, "person"); // 生成id属性 serializer.attribute(null, "id", String.valueOf(person.getId())); // <name>标签 serializer.startTag(null, "name"); // 内容 serializer.text(person.getName()); // </name>标签 serializer.endTag(null, "name"); serializer.startTag(null, "password"); serializer.text(person.getPassword()); serializer.endTag(null, "password"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); // 结束 serializer.endDocument(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } Toast.makeText(this, "success", Toast.LENGTH_SHORT).show(); }
源码地址:
https://github.com/amorypepelu/XMLDemo
时间: 2024-10-31 23:49:05