There are three methods to parse XML: sax, Dom, and pull. Dom does not have very little performance on Android, And it is preferred for Sax and pull on Android.
Some time ago I wrote a demo of Android weather forecast, involving pull parsing XML. Pull Parsing is relatively simple and easy to use,
The following is a summary of my learning experience in parsing XML using pull.
Add the weather forecast Deme code:
Http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 248543 & page = 1 & extra = # pid2291325
Obtain province information:
Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince?
Through the above address, we can use httpget to get the XML content of the province. The steps for obtaining the XML text are omitted,
The XML content is as follows:
<Arrayofstring xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns = "http://WebXml.com.cn/"> <string> municipality </string> <string> Special Administrative Region </string> <string> Heilongjiang </string> <string> jilin </string> <string> Liaoning </string> <string> Inner Mongolia </string> <string> Hebei </string> <string> Henan </string> <string> shandong </string> <string> Shanxi </string> <string> Jiangsu </string> <string> Anhui </string> <string> Shaanxi </string> <string> ningxia </string> <string> Gansu </string> <string> Qinghai </string> </arrayofstring>
1. First, use the XML static method to obtain an xmlpullparser instance of the Parser:
XmlPullParser xmlPullParser = Xml.newPullParser();
2. Set the input stream
xmlPullParser.setInput(inStream, "UTF-8");
Instream is the input stream of the obtained XML file.
3. Obtain the Event Type
int eventCode = xmlPullParser.getEventType();
The event types are commonly used as follows:
Xmlpullparser. start_document // The document starts xmlpullparser. start_tag // The tag starts. xmlpullparser. end_tag // The Tag ends xmlpullparser. end_document // The document ends
Logic:
The while statement is used to traverse the document. In each while statement, the switch is used to determine the event type and handle the event accordingly.
String name = xmlPullParser.getName();
Determine the detailed type of the tag and perform more detailed processing. At the end of each loop, use the following statement to move the cursor to the next tag for the next while loop.
eventCode = xmlPullParser.next();
Until
eventCode = XmlPullParser.END_DOCUMENT
Indicates that the document has ended, the resolution is complete.
Code:
Public static arraylist <string> getprovince (inputstream instream, int type) {arraylist <string> DATA = new arraylist <string> (); xmlpullparser = xml. newpullparser (); try {xmlpullparser. setinput (instream, "UTF-8"); int eventcode = xmlpullparser. geteventtype (); While (eventcode! = Xmlpullparser. end_document) {string name = xmlpullparser. getname (); Switch (eventcode) {Case xmlpullparser. start_document: // The document starts break; Case xmlpullparser. start_tag: // element start. if (name. equalsignorecase ("string") {If (type = 2) data. add (xmlpullparser. nexttext (). split ("\ (") [0]. trim (). tostring (); else {data. add (xmlpullparser. nexttext () ;}} break; Case xmlpullparser. end_tag: // The End Of The element break; Case xmlpullparser. end_document: // end of the Document break; default: break;} eventcode = xmlpullparser. next () ;}} catch (xmlpullparserexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}