package com.example.pullxml; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import android.os.Environment; import android.test.AndroidTestCase; import android.util.Log; import android.util.Xml; public class PullXml extends AndroidTestCase{ public void test() { writeXML(); List<Person> personList = parserXML(); for (Person person : personList) { Log.i("id", String.valueOf(person.getId())); Log.i("name", person.getName()); Log.i("age", String.valueOf(person.getAge())); } } public void writeXML() { // 获得序列化对象 XmlSerializer serializer = Xml.newSerializer(); List<Person> personList = getPersonList(); try { File sdDir = Environment.getExternalStorageDirectory(); File file = new File(sdDir, "persons.xml"); FileOutputStream fos = new FileOutputStream(file); // 指定输出位置和编码 serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true);// <?xml version='1.0' // encoding='utf-8' // standalone='true' ?> serializer.startTag(null, "persons");// <persons> for (Person person : personList) { 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(String.valueOf(person.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons");// </persons> serializer.endDocument(); } catch (Exception e) { e.printStackTrace(); } } public List<Person> parserXML() { List<Person> personList = null; Person person = null; Long id = null; // 获得pull解析对象 XmlPullParser parser = Xml.newPullParser(); try { File sdDir = Environment.getExternalStorageDirectory(); File file = new File(sdDir, "persons.xml"); FileInputStream fis = new FileInputStream(file); parser.setInput(fis, "utf-8"); // 获得事件类型 int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String tagName = parser.getName();// 获得当前节点名称 switch (eventType) { case XmlPullParser.START_TAG: if ("persons".equals(tagName)) { personList = new ArrayList<Person>(); } else if ("person".equals(tagName)) { person = new Person(); id = Long.parseLong(parser .getAttributeValue(null, "id")); person.setId(id); } else if ("name".equals(tagName)) { person.setName(parser.nextText()); } else if ("age".equals(tagName)) { person.setAge(Integer.parseInt(parser.nextText())); } break; case XmlPullParser.END_TAG: if("person".equals(tagName)){ personList.add(person); } break; } eventType=parser.next(); } } catch (Exception e) { e.printStackTrace(); return null; } return personList; } public List<Person> getPersonList() { List<Person> personList = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person person = new Person((long) i, "liang" + i, i); personList.add(person); } return personList; } } class Person { private Long id; private String name; private Integer age; public Person(Long id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Person() { super(); } @Override public String toString() { return "person [id=" + id + ", name=" + name + ", age=" + age + "]"; } public Long getId() { return id; } public void setId(Long 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; } }
时间: 2024-10-20 07:39:48