android解析XML文件的三方法之Pull(读写)
???? 除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android内置的Pull解析器解析XML文件。 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型节点的值。
1:
public void testPullGetPersons() throws Throwable{InputStream inStream = getClass().getClassLoader().getResourceAsStream("itcast.xml");List<Person> persons = PULLPersonService.getPersons(inStream);for(Person person : persons){Log.i(TAG, person.toString());}}public void testSave() throws Throwable{//File file = new File(this.getContext().getFilesDir(), "person.xml");//FileOutputStream outStream = new FileOutputStream(file);List<Person> persons = new ArrayList<Person>();persons.add(new Person(34, "lili", (short)12));persons.add(new Person(56, "老毕", (short)32));persons.add(new Person(39, "老张", (short)40));/*FileOutputStream outStream = this.getContext().openFileOutput("person.xml", Context.MODE_PRIVATE);OutputStreamWriter writer = new OutputStreamWriter(outStream, "UTF-8");BufferedWriter bWriter = new BufferedWriter(writer);*/StringWriter writer = new StringWriter();PULLPersonService.save(persons, writer);Log.i(TAG, writer.toString());}
??
?可参考:http://www.cnblogs.com/zhangdongzi/archive/2011/04/15/2016828.html
1 楼 xiaokang1582830 2012-06-06 这代码能够正常运行吗?到处都是问题..