Pull Parser: deserialization and serialization, pull serialization
Pull Parser: deserializes and reads xml files to obtain the data of an object.
1 import java. io. fileInputStream; 2 import java. io. IOException; 3 import java. util. arrayList; 4 import java. util. list; 5 6 import org. xmlpull. v1.XmlPullParser; 7 import org. xmlpull. v1.XmlPullParserException; 8 import org. xmlpull. v1.XmlPullParserFactory; 9 10 public class ReadXmlTest {11 12/** 13 * pull Parser: deserialization reads the xml file to obtain the data of an object 14 * @ param args15 * @ throws XmlPullParserException 16 * @ throws IOExcep Tion 17 */18 public static void main (String [] args) throws XmlPullParserException, IOException {19 20 // 1. guide package 21 // 2. retrieve parser factory 22 XmlPullParserFactory factory = XmlPullParserFactory. newInstance (); 23 // 3. obtain the parser object 24 XmlPullParser parser = factory based on the factory. newPullParser (); 25 // 4. set the input stream to 26 parser. setInput (new FileInputStream ("src/student. xml ")," UTF-8 "); 27 // 5. parse 28 List <Student> list = null; 29 Student stu = null; 30 // Obtain the corresponding time type 31 int type = parser. getEventType (); 32 while (type! = XmlPullParser. END_DOCUMENT) {33 // obtain the tag name 34 String tagname = parser. getName (); 35 switch (type) {36 case XmlPullParser. START_TAG: 37 // identify the tag name 38 if ("students ". equals (tagname) {39 // create a set 40 list = new ArrayList <Student> (); 41} else if ("student ". equals (tagname) {42 // create object 43 stu = new Student (); 44 // get ID attribute value 45 String id = parser. getAttributeValue (0); 46 // value 47 stu. setId (id); 48} else if ("name ". equals (tagname) {49 // obtain the text of the TAG body 50 String name = parser. nextText (); 51 // value 52 stu. setName (name); 53} else if ("age ". equals (tagname) {54 // obtain the text of the TAG body 55 String age = parser. nextText (); 56 // value: 57 stu. setAge (Integer. parseInt (age); 58} 59 break; 60 case XmlPullParser. END_TAG: 61 if ("student ". equals (tagname) {62 // Add the object to the 63 list in the set. add (stu); 64 stu = null; 65} 66 break; 67 default: 68 break; 69} 70 // step 71 parser down. next (); 72 // value 73 type = parser. getEventType (); 74} 75 // 6. output 76 System. out. println (list); 77} 78}
Bytes ------------------------------------------------------------------------------------------------
Pull Parser: serializes and writes data of an object to an xml file.
1 import java. io. fileOutputStream; 2 import java. io. IOException; 3 4 import org. xmlpull. v1.XmlPullParserException; 5 import org. xmlpull. v1.XmlPullParserFactory; 6 import org. xmlpull. v1.XmlSerializer; 7 8 public class WriteXmlTest {9 10/** pull Parser: serialize and write the data of an object to the xml file. 11 * @ param args12 * @ throws XmlPullParserException 13 * @ throws IOException 14 */15 public static void main (String [] args) throws XmlPullParserException, IOException {16 17 Student stu = new Student ("s_001", "Fei", 23); 18 // 1. guide package 19 // 2. get parser factory 20 XmlPullParserFactory factory = XmlPullParserFactory. newInstance (); 21 // 3. obtain the parser object 22 XmlSerializer serializer = factory based on the factory. newSerializer (); 23 // 4. set output stream 24 serializer. setOutput (new FileOutputStream ("src/sss. xml ")," UTF-8 "); 25 // 5. write 26 // 5.1 write Document declaration parameter 1: encoding attribute value, parameter 2: standalone attribute value 27 seriali Zer. startDocument ("UTF-8", true); 28 // 5.2 Write Start with label 29 serializer. startTag (null, "students"); 30 for (int I = 0; I <5; I ++) {31 // 5.3 write the student label 32 serializer. startTag (null, "student"); 33 34 serializer. attribute (null, "id", stu. getId (); 35 36 serializer. startTag (null, "name"); 37 serializer. text (stu. getName (); 38 serializer. endTag (null, "name"); 39 40 serializer. startTag (null, "age"); 41 serializer. text (String. ValueOf (stu. getAge (); 42 serializer. endTag (null, "age"); 43 44 serializer. endTag (null, "student"); 45} 46 // 5.4 write end tag 47 serializer. endTag (null, "students"); 48 // 5.5 write end document 49 serializer. endDocument (); 50 // response Result 51 System. out. println ("the write is complete. Please check it! "); 52} 53}
Student Class
1 public class Student { 2 3 private String id; 4 private String name; 5 private int age; 6 7 public Student() { 8 super(); 9 }10 public Student(String id, String name, int age) {11 super();12 this.id = id;13 this.name = name;14 this.age = age;15 }16 /**17 * @return the id18 */19 public String getId() {20 return id;21 }22 /**23 * @param id the id to set24 */25 public void setId(String id) {26 this.id = id;27 }28 /**29 * @return the name30 */31 public String getName() {32 return name;33 }34 /**35 * @param name the name to set36 */37 public void setName(String name) {38 this.name = name;39 }40 /**41 * @return the age42 */43 public int getAge() {44 return age;45 }46 /**47 * @param age the age to set48 */49 public void setAge(int age) {50 this.age = age;51 }52 /* (non-Javadoc)53 * @see java.lang.Object#toString()54 */55 @Override56 public String toString() {57 return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";58 }59 60 }
Student. xml
1 <? Xml version = '1. 0' encoding = 'utf-8'?> 2 3 <students> 4 <student id = 's001'> 5 <name> zhangsan </name> 6 <age> 23 </age> 7 </student> 8 9 <student id = "s002"> 10 <name> lisi </name> 11 <age> 24 </age> 12 </student> 13 14 <student id = "s003"> 15 <name> Wang Wu </name> 16 <age> 25 </age> 17 </student> 18 19 </students>
Name of the imported package (pull parser jar package ):
Kxml2-2.3.0.jar
Xmlpull_rj1_3_4c.jar