--> pull解析器是android內置的解析器,解析原理與sax類似
--> xml文件student.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 有三個學生:姓名、年齡、性別。把這三個學生的信息用xml表示出來 --> 3 <students> 4 <student name="zhangsan"> 5 <age>18</age> 6 <sex>男</sex> 7 </student> 8 <student name="lisi"> 9 <age>22</age> 10 <sex>女</sex> 11 </student> 12 <student name="wangwu"> 13 <age>20</age> 14 <sex>男</sex> 15 </student> 16 </students>
--> Student 類:
1 package com.dragon.java.pullparse; 2 3 public class Student { 4 private String name; 5 private int age; 6 private String sex; 7 8 public Student() { 9 super(); 10 } 11 12 public Student(String name, int age, String sex) { 13 super(); 14 this.name = name; 15 this.age = age; 16 this.sex = sex; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public int getAge() { 28 return age; 29 } 30 31 public void setAge(int age) { 32 this.age = age; 33 } 34 35 public String getSex() { 36 return sex; 37 } 38 39 public void setSex(String sex) { 40 this.sex = sex; 41 } 42 43 @Override 44 public String toString() { 45 return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]"; 46 } 47 48 } Student--> Test 測試類:
1 package com.dragon.java.pullparse; 2 3 import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; 4 import static org.xmlpull.v1.XmlPullParser.END_TAG; 5 import static org.xmlpull.v1.XmlPullParser.START_TAG; 6 import static org.xmlpull.v1.XmlPullParser.TEXT; 7 8 import java.io.FileReader; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserException; 15 import org.xmlpull.v1.XmlPullParserFactory; 16 17 /* 18 * pull解析器 19 */ 20 public class Test { 21 public static void main(String[] args) throws XmlPullParserException, 22 IOException { 23 List<Student> list = null; 24 Student student = null; 25 // 1、獲取一個XmlPullParser解析器 26 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 調用解析器工廠的靜態方法,創建一個解析器工廠 27 XmlPullParser parser = factory.newPullParser(); // 創建一個pull解析器 28 parser.setInput(new FileReader( 29 "D:/workspace/08-25/src/com/dragon/java/xml/students.xml")); // 設置一個輸入流,告訴要解析的xml數據的來源 30 int event = parser.getEventType(); // 獲取當前事件的類型 31 while (event != END_DOCUMENT) { // 當碰到文檔結束的時候,結束循環 32 switch (event) { 33 case START_TAG: 34 String tag = parser.getName(); 35 if (tag.equals("students")) { 36 list = new ArrayList<>(); 37 } else if (tag.equals("student")) { 38 student = new Student(); 39 student.setName(parser.getAttributeValue(null, "name")); // 參數1:是命名空間,如果命名空間,則傳入null。參數2:屬性的名字 40 list.add(student); 41 } else if (tag.equals("age")) { 42 student.setAge(Integer.parseInt(parser.nextText())); // 獲取當前事件後面的text事件,text的值 43 } else if (tag.equals("sex")) { 44 student.setSex(parser.nextText()); 45 } 46 break; 47 case END_TAG: 48 break; 49 case TEXT: 50 break; 51 default: 52 break; 53 } 54 event = parser.next(); 55 } 56 for (Student student2 : list) { 57 System.out.println(student2); 58 } 59 } 60 }
--> 也是蠻麻煩的...