Android内核使用pull解析器 所以不用导入jar包,pull解析器简单、易用、接近于手工编写XML文件
解析的XML文件为xmlpull.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <students> 3 <student id="1"> 4 <name>张MM</name> 5 <gender>女</gender> 6 <age>18</age> 7 </student> 8 <student id="2"> 9 <name>王GG</name> 10 <gender>男</gender> 11 <age>20</age> 12 </student> 13 </students>
实体类student.java
1 public class student { 2 private Integer id; 3 private String name; 4 private String gender; 5 private Integer age; 6 public Integer getId() { 7 return id; 8 } 9 public void setId(Integer id) { 10 this.id = id; 11 } 12 public String getName() { 13 return name; 14 } 15 public void setName(String name) { 16 this.name = name; 17 } 18 public String getGender() { 19 return gender; 20 } 21 public void setGender(String gender) { 22 this.gender = gender; 23 } 24 public Integer getAge() { 25 return age; 26 } 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 31 }
解析xml的读写方法xmlService.java
1 package com.bwl.service; 2 3 import java.io.InputStream; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.xmlpull.v1.XmlPullParser; 8 import org.xmlpull.v1.XmlSerializer; 9 10 import com.bwl.pojo.student; 11 12 import android.util.Xml; 13 14 public class xmlService { 15 16 public List<student> read(InputStream xml) throws Exception { 17 List<student> list = null; 18 student student = null; 19 XmlPullParser xmlPull = Xml.newPullParser(); 20 xmlPull.setInput(xml, "UTF-8");// 设置解析值 ("路径","编码") 21 int flag = xmlPull.getEventType();// 取得当前所指向的数据的事件类型 22 do { 23 switch (flag) { 24 case XmlPullParser.START_DOCUMENT:// xml文档开始节点 start document 25 // 集合实例化 26 list = new ArrayList<student>(); 27 break; 28 case XmlPullParser.START_TAG:// xml文档开始节点解析 29 if ((xmlPull.getName()).equals("student")) {// 和节点内容进行比较 30 int id = new Integer(xmlPull.getAttributeValue(0));// 获取节点的第一个属性值 31 student = new student(); 32 student.setId(id); 33 } 34 if ((xmlPull.getName()).equals("name")) { 35 String name=xmlPull.nextText();// 取得节点的下一个Text节点 36 student.setName(name); 37 } 38 if ((xmlPull.getName()).equals("gender")) { 39 String gender=xmlPull.nextText(); 40 student.setGender(gender); 41 } 42 if ((xmlPull.getName()).equals("age")) { 43 int age=new Integer(xmlPull.nextText()); 44 student.setAge(age); 45 } 46 break; 47 case XmlPullParser.END_TAG:// xml结束节点 end document 48 if (xmlPull.getName().equals("student")) {// 节点结束判断 49 list.add(student);// student添加到list集合中 50 student = null;// student置为空 51 } 52 break; 53 } 54 flag = xmlPull.next();// 指向下一个节点 55 } while (flag != XmlPullParser.END_DOCUMENT); // 执行到END DOCUMENT节点文档结束 56 xml.close(); 57 return list; 58 } 59 60 public void write(List<student> list) throws Exception { 61 XmlSerializer xmlSerializer = Xml.newSerializer(); 62 if (list != null) { 63 xmlSerializer.startDocument("UTF-8", true);//添加statrt docunment事件 64 xmlSerializer.startTag("", "students");//添加节点,参数(命名空间,节点名称)不使用命名空间的话可以使用""代替 65 for (student student : list) { 66 xmlSerializer.setProperty("", "student"); 67 xmlSerializer.attribute("", "id", student.getId().toString());//定义student节点胡属性参数:(命名空间,属性名,属性值) 68 xmlSerializer.setProperty("", "name"); 69 xmlSerializer.text(student.getName()); 70 xmlSerializer.endTag("", "student");//节点结束标记 71 xmlSerializer.setProperty("", "gender"); 72 xmlSerializer.text(student.getGender()); 73 xmlSerializer.endTag("", "gender"); 74 xmlSerializer.setProperty("", "age"); 75 xmlSerializer.text(student.getAge().toString()); 76 xmlSerializer.endTag("", "age"); 77 xmlSerializer.endTag("", "student"); 78 } 79 xmlSerializer.endDocument();//添加END DOCUMENT事件 80 81 } 82 } 83 84 }