How to parse XML in Android

Source: Internet
Author: User

Suppose I want to parse the following XML file:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Books>3     < Book Price= "99.0"Publication date= "2008">Crazy Java Handouts</ Book>4     < Book Price= "199.0"Publication date= "2009">Crazy Android Handout</ Book>5     < Book Price= "79.0"Publication date= "2010">Crazy Handouts</ Book>6 </Books>

First put the file in the Res/xml folder, if you can not manually establish, and then parse the code as follows:

1  Public voidXmlparser (View v) {2 3Xmlresourceparser Xmlresourceparser = Getresources (). GETXML (R.xml.books);//gets the parser for the XML file4         Try {5StringBuilder SB =NewStringBuilder ();6              while(Xmlresourceparser.geteventtype ()! = xmlresourceparser.end_document) {//if not at the end of the document7                 if(Xmlresourceparser.geteventtype () = = Xmlresourceparser.start_tag) {//If you encounter the start tag of a tag, start reading the class capacity8                     //get the label's signature9String TagName =xmlresourceparser.getname ();Ten                     if(Tagname.equals ("book")) { One  AString bookprice = xmlresourceparser.getattributevalue (0);//gets the value of the first property in the label, which is the value of "price" -String bookdata = xmlresourceparser.getattributevalue (1);//gets the value of the second property in the label, which is the value of "Publication date" -String bookname = Xmlresourceparser.nexttext ();//get the value of the text node, here is the name of the book the  -Sb.append ("Price:"); - sb.append (bookprice); -Sb.append ("Date of publication:"); + sb.append (bookdata); -Sb.append ("title:"); + sb.append (bookname); ASb.append ("\ n"); at                     } -  -                 } -Xmlresourceparser.next ();//gets the next event of the parser -  -             } in  - Textview.settext (sb.tostring ()); to  +}Catch(xmlpullparserexception e) { - e.printstacktrace (); the  *}Catch(IOException e) { $ e.printstacktrace ();Panax Notoginseng         } -  the}

The parsed results are as follows:

How to parse 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.