Parsing an XML file with pull

Source: Internet
Author: User

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

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.