Android Pull方式解析XML

 

除了可以使用 SAX DOM 解析 XML 文件,大家也可以使用 Android 内置的 Pull 解析器解析 XML 文件。 Pull 解析器 的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用 parser.next() 以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个 switch 对感兴趣的事件 进行处理。当元素开始解析时,调用 parser.nextText() 方法可以获取下一个 Text 类型元素的值。
lpackage com.gao.service; import java.io.InputStream; import java.io.Writer; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import com.gao.domain.Person; public class PullPersonService { public static void writeXML(List<Person> personList,Writer writer)throws Exception { XmlSerializer serializer=Xml.newSerializer(); serializer.setOutput(writer); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for (Person person : personList){ serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(person.getId())); 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"); } public static List<Person> readXML(InputStream inputStream) throws Exception { List<Person> personList = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(inputStream, "UTF-8"); int eventCode = parser.getEventType(); Person person = null; while (eventCode != XmlPullParser.END_DOCUMENT) { switch (eventCode) { case XmlPullParser.START_DOCUMENT: personList = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: if ("person".equals(parser.getName())) { person = new Person(); person.setId(new Integer(parser.getAttributeValue(0))); } else if (person != null) { if ("name".equals(parser.getName())) { person.setName(parser.nextText()); } else if ("age".equals(parser.getName())) { person.setAge(new Integer(parser.nextText())); } } break; case XmlPullParser.END_TAG: if ("person".equals(parser.getName()) && person != null) { personList.add(person); } default: break; } eventCode = parser.next(); } return personList; } }  
测试:
public void testReadPullXML() throws Exception { InputStream inputStream=XMLPersonServiceTest.class.getClassLoader().getResourceAsStream("gao.xml"); List<Person> persons=PullPersonService.readXML(inputStream); for (Person person : persons) { Log.i(TAG, person.toString()); } } public void testWritePullXML() throws Exception { FileOutputStream fileOutputStream=this.getContext().openFileOutput("person.xml", Context.MODE_WORLD_READABLE); OutputStreamWriter writer=new OutputStreamWriter(fileOutputStream, "UTF-8"); BufferedWriter bufferedWriter=new BufferedWriter(writer); List<Person> personList=new ArrayList<Person>(); personList.add(new Person(12,"Gao",34)); personList.add(new Person(34,"高",23)); personList.add(new Person(34,"GaoMatrix",34)); PullPersonService.writeXML(personList, bufferedWriter); /*StringWriter writer=new StringWriter(); List<Person> personList=new ArrayList<Person>(); personList.add(new Person(12,"Gao",34)); personList.add(new Person(34,"高",23)); personList.add(new Person(34,"GaoMatrix",34)); PullPersonService.writeXML(personList, writer); Log.i(TAG, writer.toString());*/ } 

 

你可能感兴趣的:(android,exception,xml,null,Integer,Class)