Android Learning note 36--using the original XML file

Source: Internet
Author: User

XML file

Using an XML file in Android requires the developer to manually create the Res/xml folder.

Examples are as follows:

Book.xml==><?xml version= "1.0" encoding= "Utf-8"?><books> <book publishdate= "2016.05.05" price= " 88.6 ">android Study notes </book> <book publishdate=" 2016.06.06 "price=" 88.6 ">android decryption </book> < Book publishdate= "2016.08.08" price= "88.6" >android in layman </book></books> layout file = = "<linearlayout xmlns : android= "http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" Android: Layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= ". M Ainactivity "> <button android:id=" @+id/btntest "android:layout_width=" Wrap_content "Android : layout_height= "wrap_content" android:layout_gravity= "Center_horizontal" android:text= "Test" > </Bu tton> <edittext android:id= "@+id/edit" android:layout_width= "Match_parent" android:layout_he ight= "Wrap_content" > </edittext></linearlayout> Code Implementation = = "Package Com.example.myxml;import org.xmlpull.v1.xmlpullparserexception;import Android. R.xml;import Android.os.bundle;import Android.app.activity;import Android.content.res.xmlresourceparser;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.edittext;public class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button btntest = (button) This.findviewbyid (r.id.btntest); Btntest.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {Xmlresourceparser XRP = getresources (). GETXML (R.xml.books); StringBuilder sb = new StringBuilder (); Try{while (Xrp.geteventtype ()! = xmlresourceparser.end_document) {if ( Xrp.geteventtype () = = Xmlresourceparser.start_tag)//If you encounter the start tag {//String TagName = Xrp.getname (); if (Xrp.getname (). Equals ("book")) {//Get property value by name Sb.append ("Price:"); Sb.append (Xrp.getattributevalue (NULL, "price"); Sb.append ("\ n");//Gets the attribute value Sb.append ("Publishdate: Sb.append (xrp.getattributevalue (0)), sb.append ("\ n"), Sb.append ("BookName:");//Gets the text information of the XML node Sb.append ( Xrp.nexttext ());} Sb.append ("\ n");} Xrp.next ();//Gets the next event of the parser}edittext edit = (EditText) Findviewbyid (R.id.edit); Edit.settext (sb.tostring ());} catch (Exception e) {e.printstacktrace ();}}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is pre Sent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Operating effect:

Android Learning note 36--using the original XML file

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.