android第十二步Pull解析器解析和生成XML内容
重点关键字
InputStream xml = getClass().getClassLoader().getResourceAsStream("person.xml"); 获取包下文件
XmlPullParser parser = Xml.newPullParser(); 实例化pull
parser.setInput(xml, "UTF-8"); 设置读取位置
parser.getName()获取标签名称
parser.getAttributeValue(0)获取标签第一个属性值
parser.nextText()获取标签值
public static List<Person> getPersons(InputStream xml) throws Exception{ List<Person> persons = null; XmlPullParser pullParser= XmlPullParserFactory.newInstance().newPullParser(); //XmlPullParser pullParser=Xml.newPullParser(); pullParser.setInput(xml, "UTF-8");//为Pull解析器设置要解析的XML数据 int event = pullParser.getEventType(); Person person = null; while(event!=XmlPullParser.END_DOCUMENT) { switch(event) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: 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); person = null; } break; } event=pullParser.next(); //指针指向下一个节点 } //首先把XML数据读到char[]中然后开始解析,一个一个的字符往后读取,比对语法是否符合XML语法 return persons; } public static void save(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()); 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(); }
public class Person { @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } 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; } 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(){ } }
public class PersonServiceTest extends AndroidTestCase { private static final String TAG= "PersonServiceTest"; 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(TAG, person.toString()); } } public void testSaveXml() throws Exception{ List<Person> persons = new ArrayList<Person>(); persons.add(new Person(1,"zhangxx",80)); persons.add(new Person(4,"zhlili",80)); persons.add(new Person(8,"xiaoxiao",5)); File xmlfile = new File(getContext().getFilesDir(),"zhao.xml"); FileOutputStream outStream = new FileOutputStream(xmlfile); PersonService.save(persons, outStream); } }