在android系统提供的Pull解析器,它提供了类似的事件,如: 开始元素和结束元素事件,使用parser.next() 可以进入下一个元素并触发相应事件。跟 SAX 不同的 是, Pull 解析器产生的事件是一个数字,而非方法,因此可以使用一个 switch 对感兴趣的事件 进行处理。当元素开始解析时,调用 parser.nextText() 方法可以获取下一个 Text 类型节点的值。
private List<Person> parseXML(InputStream in) { List<Person> list = null; Person person = null; try { XmlPullParser newPullParser = XmlPullParserFactory.newInstance().newPullParser(); // XmlPullParser parse = Xml.newPullParser(); 另一种方法 // 设置读取路径和编码 newPullParser.setInput(in, "utf-8"); int eventType = newPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: String name = newPullParser.getName(); if(TextUtils.equals("persons", name)) { list = new ArrayList<Person>(); } else if(TextUtils.equals("person", name)) { person = new Person(); String id = newPullParser.getAttributeValue(null, "id"); person.setId(Integer.parseInt(id)); } else if(TextUtils.equals("name", name)) { String personName = newPullParser.nextText(); person.setName(personName); } else if(TextUtils.equals("age", name)) { String age = newPullParser.nextText(); person.setAge(Integer.parseInt(age)); } break; case XmlPullParser.END_TAG: String endTagName = newPullParser.getName(); if(TextUtils.equals("person", endTagName)) { list.add(person); } break; case XmlPullParser.END_DOCUMENT: break; default: break; } eventType = newPullParser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.createXMl: File file = new File(Environment.getExternalStorageDirectory(), "persons.xml"); try { FileOutputStream out = new FileOutputStream(file); createXML(out); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.parseXML: File fileIn = new File(Environment.getExternalStorageDirectory(), "persons.xml"); try { FileInputStream in = new FileInputStream(fileIn); List<Person> list = parseXML(in); for(Person person : list) { Log.i("MainActivity", person.toString()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } } public class Person { public int id; public String name; public int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } } private void createXML(OutputStream os) { try { // 创建XML生成器 XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer(); // XmlSerializer newSerializer = Xml.newSerializer(); 另一种方法 // 设置输出路径和编码 serializer.setOutput(os, "utf-8"); // 开始文档 serializer.startDocument("utf-8", null); // 开始标记 serializer.startTag(null, "persons"); serializer.startTag(null, "person"); // 设置属性 serializer.attribute(null, "id", "1"); serializer.startTag(null, "name"); // 赋值 serializer.text("张三"); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text("18"); serializer.endTag(null, "age"); serializer.endTag(null, "person"); serializer.startTag(null, "person"); serializer.attribute(null, "id", "2"); serializer.startTag(null, "name"); serializer.text("李四"); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text("30"); serializer.endTag(null, "age"); serializer.endTag(null, "person"); serializer.startTag(null, "person"); serializer.attribute(null, "id", "3"); serializer.startTag(null, "name"); serializer.text("王武"); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text("108"); serializer.endTag(null, "age"); serializer.endTag(null, "person"); // 结束标记 serializer.endTag(null, "persons"); // 结束文档 serializer.endDocument(); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }