Pull parsing of XML in Android

Source: Internet
Author: User

Pull parsing FOR XML:


//class loader loading XML file

InputStream is = MainActivity.class.getClassLoader (). getResourceAsStream ("Weather.xml");

Pull parser to generate XML

Xmlpullparser pull = Xml.newpullparser ();

try {

Setting the input stream

Pull.setinput (IS, "utf-8");

The state that the parser is currently in

int type = Pull.geteventtype ();

Weather Weather = null;

while (type! = xmlpullparser.end_document) {

Switch (type) {

Node that is currently the start tag

Case Xmlpullparser.start_tag:

Get the name of the node tag

String name = Pull.getname ();

Determine the name of the node tag (below) and place it in the weather object

if ("Channel". Equals (name)) {

weather = new weather ();

Weather.setid (pull.getattributevalue (0));

        }


if ("City". Equals (name)) {

Weather.setname (Pull.nexttext ());

        }

if ("Temp". Equals (name)) {

weather.settemp (Pull.nexttext ());

        }

if ("Wind". Equals (name)) {

Weather.setwind (Pull.nexttext ());

        }

break;

If this is the end tag node, place the Weather object in the list collection to save

Case Xmlpullparser.end_tag:

String tagname = Pull.getname ();

if ("Channel". Equals (tagname)) {

List.add (weather);

    }

Default

break;

}

Must be written here, otherwise it will not be executed down

Type = Pull.next ();

}

} catch (Exception e) {

E.printstacktrace ();

}

}



This article is from the "Beginner" blog, so be sure to keep this source http://10154241.blog.51cto.com/10144241/1664205

Pull parsing of XML in Android

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.