Android Pull解析XML
在上文中介绍了使用sax方式解析xml,这里介绍下在Android中极力推荐的xmlpull方式解析xml。xmlpull不仅仅可以使用在Android上,同样也适用于javase,但在javase环境下,你需要自己去获取xmlpull所依赖的类库。 环境:
kxml2-2.3.0.jar
xmlpull_1_1_3_4c.jar
以上两个jar分别从以下网站下载:
http://www.xmlpull.org/ xmlpull
http://kxml.sourceforge.net/ kxml
xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <root> 3 <student id="1" group="1"> 4 <name>张三</name> 5 <sex>男</sex> 6 <age>18</age> 7 <email>zhangsan@163.com</email> 8 <birthday>1987-06-08</birthday> 9 <memo>好学生</memo> 10 </student> 11 <student id="2" group="2"> 12 <name>李四</name> 13 <sex>女</sex> 14 <age>18</age> 15 <email>lisi@163.com</email> 16 <birthday>1987-06-08</birthday> 17 <memo>好学生</memo> 18 </student> 19 <student id="3" group="3"> 20 <name>小王</name> 21 <sex>男</sex> 22 <age>18</age> 23 <email>xiaowang@163.com</email> 24 <birthday>1987-06-08</birthday> 25 <memo>好学生</memo> 26 </student> 27 <student id="4" group="4"> 28 <name>小张</name> 29 <sex>男</sex> 30 <age>18</age> 31 <email>xiaozhang@163.com</email> 32 <birthday>1987-06-08</birthday> 33 <memo>好学生</memo> 34 </student> 35 <student id="5" group="5"> 36 <name>小明</name> 37 <sex>男</sex> 38 <age>18</age> 39 <email>xiaoming@163.com</email> 40 <birthday>1987-06-08</birthday> 41 <memo>好学生</memo> 42 </student> 43 </root>
Xml对应的实体bean文件
1 package com.parsexml.entity; 2 3 public class Student { 4 5 private int id; 6 private int group; 7 private String name; 8 private String sex; 9 private int age; 10 private String email; 11 private String memo; 12 private String birthday; 13 public int getId() { 14 return id; 15 } 16 public void setId(int id) { 17 this.id = id; 18 } 19 public int getGroup() { 20 return group; 21 } 22 public void setGroup(int group) { 23 this.group = group; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public String getSex() { 32 return sex; 33 } 34 public void setSex(String sex) { 35 this.sex = sex; 36 } 37 public int getAge() { 38 return age; 39 } 40 public void setAge(int age) { 41 this.age = age; 42 } 43 public String getEmail() { 44 return email; 45 } 46 public void setEmail(String email) { 47 this.email = email; 48 } 49 public String getMemo() { 50 return memo; 51 } 52 public void setMemo(String memo) { 53 this.memo = memo; 54 } 55 public String getBirthday() { 56 return birthday; 57 } 58 public void setBirthday(String birthday) { 59 this.birthday = birthday; 60 } 61 62 }
PUll解析代码
1 package com.parsexml; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.xmlpull.v1.XmlPullParser; 8 import org.xmlpull.v1.XmlPullParserException; 9 import org.xmlpull.v1.XmlPullParserFactory; 10 11 import com.parsexml.entity.Student; 12 13 public class PullParseXml { 14 15 16 public List<Student> PullParseXML(){ 17 18 List<Student> list=null; 19 Student student = null; 20 21 //构建XmlPullParserFactory 22 try { 23 XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance(); 24 //获取XmlPullParser的实例 25 XmlPullParser xmlPullParser=pullParserFactory.newPullParser(); 26 //设置输入流 xml文件 27 xmlPullParser.setInput(Thread.currentThread().getContextClassLoader().getResourceAsStream("Student.xml"), "UTF-8"); 28 29 //开始 30 int eventType=xmlPullParser.getEventType(); 31 32 try { 33 while(eventType!=XmlPullParser.END_DOCUMENT){ 34 String nodeName=xmlPullParser.getName(); 35 switch (eventType) { 36 //文档开始 37 case XmlPullParser.START_DOCUMENT: 38 list=new ArrayList<Student>(); 39 break; 40 //开始节点 41 case XmlPullParser.START_TAG: 42 //判断如果其实节点为student 43 if("student".equals(nodeName)){ 44 //实例化student对象 45 student=new Student(); 46 //设置Id属性 47 student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0))); 48 //设置Group属性 49 student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1))); 50 }else if("name".equals(nodeName)){ 51 //设置name 52 student.setName(xmlPullParser.nextText()); 53 }else if("sex".equals(nodeName)){ 54 //设置sex 55 student.setSex(xmlPullParser.nextText()); 56 }else if("age".equals(nodeName)){ 57 //设置age 58 student.setAge(Integer.parseInt(xmlPullParser.nextText())); 59 }else if("email".equals(nodeName)){ 60 //设置email 61 student.setEmail(xmlPullParser.nextText()); 62 }else if("birthday".equals(nodeName)){ 63 //设置birthday 64 student.setBirthday(xmlPullParser.nextText()); 65 }else if("memo".equals(nodeName)){ 66 //设置memo属性 67 student.setMemo(xmlPullParser.nextText()); 68 } 69 break; 70 //结束节点 71 case XmlPullParser.END_TAG: 72 if("student".equals(nodeName)){ 73 list.add(student); 74 student=null; 75 } 76 break; 77 default: 78 break; 79 } 80 eventType=xmlPullParser.next(); 81 } 82 } catch (NumberFormatException e) { 83 e.printStackTrace(); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 } catch (XmlPullParserException e) { 88 e.printStackTrace(); 89 } 90 91 return list; 92 } 93 94 public static void main(String[] args) { 95 List<Student> list=new PullParseXml().PullParseXML(); 96 for(Student student:list){ 97 System.out.println("id:"+student.getId()+"\tgroup:"+student.getGroup()+"\tname:"+student.getName()+"\tsex:"+student.getSex()+"\tage:"+student.getAge()+"\temail:"+student.getEmail()+"\tbirthday:"+student.getBirthday()+"\tmemo:"+student.getMemo()); 98 } 99 } 100 101 }
转:http://www.blogjava.net/sxyx2008/archive/2010/08/04/327885.html