XML parsing by Android xmlpullparser

Source: Internet
Author: User
Public void getxml (string URL) throws xmlpullparserexception, ioexception, urisyntaxexception {string xmlstring = downloadxml (URL); xmlpullparserfactory factory = xmlpullparserfactory. newinstance (); factory. setnamespaceaware (true); xmlpullparser parser = factory. newpullparser (); parser. setinput (New stringreader (xmlstring); int eventtype = parser. geteventtype (); List <forecast_conditions> conditionslist = new Arraylist <forecast_conditions> (); forecast_conditions = NULL; while (eventtype! = Xmlpullparser. end_document) {If (eventtype = xmlpullparser. start_document) {system. out. println ("START document");} else if (eventtype = xmlpullparser. start_tag) {system. out. println ("start tag" + parser. getname (); If (parser. getname (). equals ("forecast_conditions") {forecast_conditions = new forecast_conditions ();} If (parser. getname (). equals ("day_of_week") {forecast_conditions.setday_of_week (parser. getattributevalue (0);} else if (parser. getname (). equals ("low") {forecast_conditions.setlow (parser. getattributevalue (0);} else if (parser. getname (). equals ("high") {forecast_conditions.sethigh (parser. getattributevalue (0);} else if (parser. getname (). equals ("icon") {try {forecast_conditions.seticon (parser. getattributevalue (0);} catch (exception e) {// todo: handle exception} else if (parser. getname (). equals ("condition") {try {forecast_conditions.setcondition (parser. getattributevalue (0);} catch (exception e) {}}/* int size = parser. getattributecount (); For (INT I = 0; I <size; I ++) {system. out. println ("attributename" + parser. getattributename (I); system. out. println ("attributevalue" + parser. getattributevalue (I);} */} else if (eventtype = xmlpullparser. end_tag) {system. out. println ("end tag" + parser. getname (); If (parser. getname (). equals ("forecast_conditions") {conditionslist. add (forecast_conditions) ;}} else if (eventtype = xmlpullparser. text) {system. out. println ("text" + parser. gettext ();} eventtype = parser. next ();} system. out. println ("end document"); bindtolistview (conditionslist); // xmlutils .}
 
 
This instance is incomplete. Android beginners are not standardized in many places. For reference only.

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.