1 Publiclist<person> decodexml (String filename)throwsxmlpullparserexception2 {3 //gets the asset object for the current program4Assetmanager am =context.getassets ();5 6List<person> pList =NewArraylist<person>();7 8 Try {9 Ten //opens a file in the asset file as filename and returns the data as a InputStream stream OneInputStream is =am.open (filename); A - //new A Xmlpullparser object instance -Xmlpullparser xpp =Xml.newpullparser (); the //initializes the new Xmlpullparser instance object with the XML file stream to parse -Xpp.setinput (IS, "Utf-8"); - - //Start parsing + intType = Xpp.geteventtype ();//get the current event type of xpp -Person person =NULL; + A while(type!= xmlpullparser.end_document) {//occurs when the current node is not equal to the end of the XML document at - if(type = = Xmlpullparser.start_tag)//When the current tag is the start tag of an information node, this is the beginning of a person node - { - //Create a Person object to store data -person =NewPerson (); - in if("Person". Equals (Xpp.getname ())) {//The current type is Yes <person> tag Yes - toPerson.setid (Integer.parseint (xpp.getattributevalue (0)));//gets the first property of the current label + } - Else if("Name". Equals (Xpp.getname ())) { the *Person.setname (Xpp.nexttext ());//get the contents of the current label pair, such as <name>aaa</name> get the content is AAA $ }Panax Notoginseng Else if("Age". Equals (Xpp.getname ())) { - the person.setage (Integer.parseint (Xpp.nexttext ())); + } A Else if("Sex". Equals (Xpp.getname ())) { the + Person.setsex (Xpp.nexttext ()); - } $ } $ - if(type = = Xmlpullparser.end_tag) {//when the current type is the end tag of an information node, here is the </pserson> - the Plist.add (person); - }Wuyi the //Skip to next tab -Type =Xpp.next (); Wu } - About returnpList; $ -}Catch(IOException e) { - - e.printstacktrace (); A return NULL; + } the -}
Parsing an XML file with pull