java还提供SAX和DOM用于解析XML
Android还集成了Pull解析器——推荐
package cn.itcast.service; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import cn.itcast.domain.Person; public class PersonService { /** * 读取数据 * @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"); int event = pullparser.getEventType(); while (event!=pullparser.END_DOCUMENT){ switch (event) { case XmlPullParser.START_DOCUMENT: // 数据初始化 persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: // 数据read if ("person".equals(pullparser.getName())){ int id = new Integer(pullparser.getAttributeValue(0)); person = new Person(); person.setId(id); } if ("name".equals(pullparser.getName())) { String name = pullparser.nextText(); person.setName(name); } if ("age".equals(pullparser.getName())) { int age = new Integer(pullparser.nextText()); person.setAge(age); } break; case XmlPullParser.END_TAG: if ("person".equals(pullparser.getName())){ persons.add(person); } break; default: break; } event = pullparser.next(); } return persons; } /** * 保存数据 * @param persons * @param out * @throws Exception */ public static void savePersons(List<Person> persons, OutputStream out) throws Exception{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true); 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().toString()); 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(); } }
// 单元测试
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.itcast.xml" /> <application <uses-library android:name="android.test.runner" />
public class PersonServiceTest extends AndroidTestCase { public void testPersons() throws Exception{ InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons = PersonService.getPersons(xml); for (Person person:persons){ Log.i("test111", person.toString()); } } public void testSave() throws Exception{ List<Person> persons = new ArrayList<Person>(); persons.add(new Person(10, "10", 10)); persons.add(new Person(20, "20", 20)); File xml = new File(getContext().getFilesDir(), "itcast.xml"); FileOutputStream outputStream = new FileOutputStream(xml); PersonService.savePersons(persons, outputStream); outputStream.close(); } }
// 数据结构
package cn.itcast.domain; public class Person { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Person(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Person() {} }