Java --> xml pull parsing, java -- xmlpull
--> The pull parser is a built-in android parser. The parsing principle is similar to that of the sax Parser.
--> Xml file student. xml:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! -- There are three students: name, age, and gender. Show the information of the three students in xml --> 3 <students> 4 <student name = "zhangsan"> 5 <age> 18 </age> 6 <sex> male </sex> 7 </student> 8 <student name = "lisi"> 9 <age> 22 </age> 10 <sex> female </sex> 11 </student> 12 <student name = "wangwu"> 13 <age> 20 </age> 14 <sex> male </sex> 15 </student> 16 </students>
--> Student class:
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 @ Override44 public String toString () {45 return "Student [name =" + name + ", age =" + age + ", sex = "+ sex +"] "; 46} 47 48}Student
--> Test class:
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 parser 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. Get an XmlPullParser parser 26 XmlPullParserFactory factory = XmlPullParserFactory. newInstance ();// Call the static method of the parser factory to create a parser factory 27 XmlPullParser parser = factory. newPullParser (); // create a pull parser 28 parser. setInput (new FileReader (29 "D:/workspace/08-25/src/com/dragon/java/xml/students. xml "); // set an input stream to tell the source of the xml data to be parsed 30 int event = parser. getEventType (); // get the current event type 31 while (event! = END_DOCUMENT) {// when a document ends, the 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"); // parameter 1: it is a namespace. If it is a namespace, null is input. Parameter 2: attribute name 40 list. add (student); 41} else if (tag. equals ("age") {42 student. setAge (Integer. parseInt (parser. nextText (); // obtain the text event next to the current event. The value of text is 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}
--> It is also quite troublesome...