<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>Liri</name> <age>30</age> </person> <person id="20"> <name>Liyue</name> <age>25</age> </person> </persons>
package org.fun.domain; public class Person { private Integer id; private String name; private Short 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 Short getAge() { return age; } public void setAge(Short age) { this.age = age; } }
package org.fun.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.fun.domain.Person; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXPersonService { public List<Person> getPersons(InputStream inStream) throws Throwable { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); PersonParser personParser = new PersonParser(); parser.parse(inStream, personParser); return personParser.getPersons(); } private final class PersonParser extends DefaultHandler { private List<Person> persons = null; private String tag = null; private Person person = null; public List<Person> getPersons() { return persons; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (tag != null) { String data = new String(ch, start, length); if ("name".equals(tag)) { person.setName(data); } else if ("age".equals(tag)) { person.setAge(new Short(data)); } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if ("person".equals(localName)) { persons.add(person); person = null; } tag = null; } @Override public void startDocument() throws SAXException { persons = new ArrayList<Person>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("person".equals(localName)) { person = new Person(); person.setId(new Integer(attributes.getValue(0))); } tag = localName; } } }
package org.fun.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.fun.domain.Person; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMPersonService { public static List<Person> getPersons(InputStream inStream) throws Throwable { List<Person> persons = new ArrayList<Person>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inStream); Element root = document.getDocumentElement(); NodeList personNodes = root.getElementsByTagName("person"); for (int i = 0; i < personNodes.getLength(); i++) { Person person = new Person(); Element personElement = (Element) personNodes.item(i); person.setId(new Integer(personElement.getAttribute("id"))); NodeList personChilds = personElement.getChildNodes(); for (int j = 0; j < personNodes.getLength(); j++) { if (personChilds.item(j).getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) personChilds.item(j); if ("name".equals(childElement.getNodeName())) { person.setName(childElement.getFirstChild() .getNodeValue()); } else if ("age".equals(childElement.getNodeName())) { person.setAge(new Short(childElement.getFirstChild() .getNodeValue())); } } } persons.add(person); } return persons; } }
package org.fun.service; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.fun.domain.Person; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; public class PullPersonService { /*====================pull生成xml文件 public static void save(List<Person> persons, OutputStream outputStream) throws Throwable { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outputStream, "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()); 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(); outputStream.flush(); outputStream.close(); } =====================================*/ public static List<Person> getPersons(InputStream inStream) throws Throwable { List<Person> persons = null; Person person = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(inStream, "utf-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: String name = parser.getName(); if ("person".equals(name)) { person = new Person(); person.setId(new Integer(parser.getAttributeValue(0))); } if (person != null) { if ("name".equals(name)) { person.setName(parser.nextText()); } if ("age".equals(name)) { person.setAge(new Short(parser.nextText())); } } break; case XmlPullParser.END_TAG: if ("person".equals(parser.getName())) { persons.add(person); person = null; } break; default: break; } eventType = parser.next(); } return persons; } }