1 Public StaticList<person> getpersons (inputstream XML)throwsexception{2list<person> persons =NULL;3Person person =NULL;4Xmlpullparser Pullparser =Xml.newpullparser ();5Pullparser.setinput (XML, "UTF-8");//To set the XML data to parse for the pull parser6 intevent =Pullparser.geteventtype ();7 while(Event! =xmlpullparser.end_document) {8 Switch(event) {9 Casexmlpullparser.start_document:TenPersons =NewArraylist<person>(); One Break; A - CaseXmlpullparser.start_tag: - if("Person". Equals (Pullparser.getname ())) { the intID =NewInteger (Pullparser.getattributevalue (0)); -person =NewPerson (); - Person.setid (ID); - } + if("Name". Equals (Pullparser.getname ())) { -String name =Pullparser.nexttext (); + person.setname (name); A } at if("Age". Equals (Pullparser.getname ())) { - intAge =NewInteger (Pullparser.nexttext ()); - Person.setage (age); - } - Break; - in CaseXmlpullparser.end_tag: - if("Person". Equals (Pullparser.getname ())) { to Persons.add (person); +person =NULL; - } the Break; * } $event =Pullparser.next ();Panax Notoginseng } - returnpersons; the}
Xmlpullparser parsing an XML file