android--parsing XML using Xmlpull

Source: Internet
Author: User

The Xmlpull way of parsing XML is strongly recommended in Android. Xmlpull can not only be used on Android. The same applies to javase, but in the javase environment. You need to get the class library that xmlpull depends on.
Environment:

Kxml2-2.3.0.jar

Xmlpull_1_1_3_4c.jar

The above two jars are downloaded from the following sites:

Http://www.xmlpull.org/xmlpull

Http://kxml.sourceforge.net/kxml

XML file:

<?

XML version= "1.0" encoding= "UTF-8"?

><root> <student id= "1" group= "1" > <name> Zhang San </name> <sex> men </sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08& Lt;/birthday> <memo> Good students </memo> </student> <student id= "2" group= "2" > <na me> John Doe </name> <sex> women </sex> <age>18</age> <email>[email pro Tected]m</email> <birthday>1987-06-08</birthday> <memo> Good students </memo> </stud Ent> <student id= "3" group= "3" > <name> xiao Wang </name> <sex> men </sex> < Age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birth Day> <memo> Good students </memo> </student> <student id= "4" group= "4" > <name> Xiao Zhang & Lt;/name> <Sex> men </sex> <age>18</age> <email>[email protected]</email> < Birthday>1987-06-08</birthday> <memo> Good students </memo> </student> <student id= "5" Grou p= "5" > <name> xiao Ming </name> <sex> men </sex> <age>18</age> <e Mail>[email protected]</email> <birthday>1987-06-08</birthday> <memo> Good students < /memo> </student></root>

XML corresponding entity Bean file:

Package Com.parsexml.entity;public class Student {private int id;    private int Group;    private String name;    Private String sex;    private int age;    Private String Email;    Private String Memo;    Private String birthday;    public int getId () {return id;    } public void setId (int id) {this.id = ID;    } public int Getgroup () {return group;    } public void Setgroup (int group) {this.group = group;    } public String GetName () {return name;    } public void SetName (String name) {this.name = name;    } public String Getsex () {return sex;    public void Setsex (String sex) {this.sex = sex;    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    } public String Getemail () {return email;    } public void Setemail (String email) {this.email = email;    } public String Getmemo () {return memo; } public void Setmemo (String memo) {This.memo = Memo;    } public String Getbirthday () {return birthday;    } public void Setbirthday (String birthday) {this.birthday = birthday; }    }

Pull parsing code:

Package Com.parsexml;import Java.io.ioexception;import Java.util.arraylist;import java.util.list;import Org.xmlpull.v1.xmlpullparser;import Org.xmlpull.v1.xmlpullparserexception;import Org.xmlpull.v1.xmlpullparserfactory;import Com.parsexml.entity.student;public class PullParseXml {public List& Lt        Student> Pullparsexml () {list<student> list=null;                Student Student = null; Build xmlpullparserfactory try {xmlpullparserfactory pullparserfactory=xmlpullparserfactory.newinstance ()            ;            Get an instance of Xmlpullparser Xmlpullparser Xmlpullparser=pullparserfactory.newpullparser (); Sets the input stream XML file Xmlpullparser.setinput (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Stude                        Nt.xml ")," UTF-8 ");                        Start int eventtype=xmlpullparser.geteventtype ();               try {while (eventtype!=xmlpullparser.end_document) {     String Nodename=xmlpullparser.getname ();                        Switch (EventType) {//document start case xmlpullparser.start_document:                        List=new arraylist<student> ();                    Break                        Start node case Xmlpullparser.start_tag://Inference hypothesis The fact that the node is student if ("Student". Equals (NodeName)) {//Instantiate Student Object Student=new student ()                            ;                            Set the id attribute Student.setid (integer.parseint (xmlpullparser.getattributevalue (0)));                        Set the Group Property Student.setgroup (Integer.parseint (Xmlpullparser.getattributevalue (1))); }else if ("Name". Equals (NodeName)) {//Set name S                        Tudent.setname (Xmlpullparser.nexttext ()); }else if ("Sex". Equals (NodeName)) {//Set sex Student.setsex (Xmlpullparser.nexttext ()); }else if ("Age". Equals (NodeName)) {//Set age student.                        Setage (Integer.parseint (Xmlpullparser.nexttext ())); }else if ("email". Equals (NodeName)) {//Set email Student.setemail (xmlpu                        Llparser.nexttext ()); }else if ("Birthday". Equals (NodeName)) {//Set birthday Student.setbirth                        Day (Xmlpullparser.nexttext ()); }else if ("Memo". Equals (NodeName)) {//Set Memo property Student.setmemo (Xmlpul                        Lparser.nexttext ());                    } break;              End Node Case XmlPullParser.END_TAG:if ("Student". Equals (NodeName)) {              List.add (student);                        Student=null;                    } break;                    Default:break;                } eventtype=xmlpullparser.next ();            }} catch (NumberFormatException e) {e.printstacktrace ();            } catch (IOException e) {e.printstacktrace ();        }} catch (Xmlpullparserexception e) {e.printstacktrace ();    } return list; public static void Main (string[] args) {list<student> list=new pullparsexml ().        Pullparsexml (); for (Student student:list) {System.out.println ("ID:" +student.getid () + "\tgroup:" +student.getgroup () + "\tname:" +stu Dent.getname () + "\tsex:" +student.getsex () + "\tage:" +student.getage () + "\temail:" +student.getemail () + "\tbirthday:"        +student.getbirthday () + "\tmemo:" +student.getmemo ()); }    }    }

Note: If the Kxml2-2.3.0.jar file is missing from project, the following exception message is reported:

You should have joined the Kxml2-2.3.0.jar bag to your classpath.




android--parsing XML using Xmlpull

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.