Android learning Experience (1)--pull parser

Source: Internet
Author: User

The Android kernel uses the pull parser so you don't have to import the jar package, the pull parser is simple, easy to use, close to hand-written XML files

Parse the XML file as Xmlpull.xml

  

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Students>3     <StudentID= "1">4         <name>Zhang mm</name>5         <Gender>Woman</Gender>6         < Age>18</ Age>7     </Student>8     <StudentID= "2">9         <name>Wang Gg</name>Ten         <Gender>Man</Gender> One         < Age>20</ Age> A     </Student> - </Students>

Entity class 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) {Ten this.id = ID; One     } A Public String GetName () { - return name; -     } the Public void SetName (String name) { - this.name = name; -     } - Public String Getgender () { + return gender; -     } + Public void Setgender (String gender) { A This.gender = gender; at     } - Public Integer Getage () { - return age; -     } - Public void Setage (Integer age) { - this.age = age; in     } -      to}

 

Parsing XML Read-write method 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 Ten import com.bwl.pojo.student; One  A import android.util.Xml; -  - Public class Xmlservice { the  -Public List<Student>Read (InputStream xml) throws Exception { -List<Student>list = null; - student student = null; + Xmlpullparser xmlpull = Xml.newpullparser (); - xmlpull.setinput (XML, "UTF-8");//Set parse value ("Path", "encoding") + int flag = Xmlpull.geteventtype ();//The event type that gets the data currently pointing to A Do { at switch (flag) { - Case xmlpullparser.start_document://XML document starting node start document - //Collection instantiation -List = new ArrayList<Student>(); - Break ; - Case xmlpullparser.start_tag://XML document start node parsing in if ((Xmlpull.getname ()). Equals ("Student")) {//and node contents are compared - int id = new Integer (xmlpull.getattributevalue (0));//Gets the first property value of a node to student = new Student (); + Student.setid (ID); -                 } the if ((Xmlpull.getname ()). Equals ("name")) { * String Name=xmlpull.nexttext ();//Gets the next text node of the node $ student.setname (name);Panax Notoginseng                 } - if ((Xmlpull.getname ()). Equals ("gender")) { the String Gender=xmlpull.nexttext (); + Student.setgender (gender); A                 } the if ((Xmlpull.getname ()). Equals ("Age")) { + int age=new Integer (Xmlpull.nexttext ()); - Student.setage (age); $                 } $ Break ; - Case xmlpullparser.end_tag://XML end node End document - if (Xmlpull.getname (). Equals ("Student")) {//node end judgment the List.add (student);//student added to the list collection - student = null;//student is emptyWuyi                 } the Break ; -             } Wu flag = Xmlpull.next ();//refers to the next node - } while (flag! = xmlpullparser.end_document); Execution to end document node documentation ends About xml.close (); $ return list; -     } -  -public void Write (List<Student>list) throws Exception { A XmlSerializer XmlSerializer = Xml.newserializer (); + if (list! = null) { the xmlserializer.startdocument ("UTF-8", true);//Add STATRT docunment event - Xmlserializer.starttag ("", "students");//Add nodes, parameters (namespaces, node names) if you don't use namespaces, you can use "" instead $ For (student student:list) { the Xmlserializer.setproperty ("", "student"); the Xmlserializer.attribute ("", "id", Student.getid (). toString ());//define Student Node Hu attribute parameter: (namespace, property name, property value) the Xmlserializer.setproperty ("", "name"); the Xmlserializer.text (Student.getname ()); - Xmlserializer.endtag ("", "student");//node end tag in Xmlserializer.setproperty ("", "gender"); the Xmlserializer.text (Student.getgender ()); the Xmlserializer.endtag ("", "gender"); About Xmlserializer.setproperty ("", "Age"); the Xmlserializer.text (Student.getage (). toString ()); the Xmlserializer.endtag ("", "Age"); the Xmlserializer.endtag ("", "student"); +             } - xmlserializer.enddocument ();//Add End document event the Bayi         } the     } the  -}

  

Android learning Experience (1)--pull parser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.