Android XML data parsing (pull)

Source: Internet
Author: User

XML to be parsed:

<? XML version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> instructor Zhang </Name>
<Age> 21 </age>
<Persion>
<Person id = "20">
<Name> instructor Li </Name>
<Age> 25 </age>
<Persion>

</Persons>


The jar package where the response needs to be downloaded: http://www.xmlpull.org/v1/download/unpacked/doc/

Main java files:

// Create a factory for XML Parsing
Xmlpullparserfactory factory = xmlpullparserfactory. newinstance ();
// Obtain the reference of the XML parsing class
Xmlpullparser parser = factory. newpullparser ();
// Input the XML data to be parsed as a stream
Parser. setinput (Java. Io. Reader in );
// Obtain the Event Type
Int eventtype = parser. geteventtype ();
// Determine whether the end of the document is reached
While (eventtype! = Xmlpullparser. end_document ){
Switch (eventtype ){
// Start reading the document
Case xmlpullparser. start_document:
Break;
// Encounter tag Elements
Case xmlpullparser. start_tag:
If ("person". Equals (parser. getname ())){
// Retrieve the attribute value. 0 indicates 0th attributes.
Int id = integer. parseint (parser. getattributevalue (0 ));
} Else if ("name". Equals (parser. getname ())){
// Obtain the content of the node
String name = parser. nettext ();
} Else if ("Age". Equals (parser. getname ())){
// Obtain the content of the node
String age = parser. nettext ();
}
Break;
// Tag ends
Case xmlpullparser. end_tag:
If ("person". Equals (parser. getname ())){
// Initialization can be performed here
}
Break;
}
// Loop
Eventtype = parser. Next ();

}

Related Article

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.