XML resources used by Android Resources

Source: Internet
Author: User

XML files that need to be read as resources are usually placed in the res/xml Directory, which will be compiled into binary files during packaging. XmlPullParser objects are used to access these xml files.

Resources res = activity.getResources(); XmlResourceParser xpp = res.getXml(R.xml.test); 

XmlResourceParser is an instance of XmlPullParser.

The following is the code for XmlPullParser to access the XML node.

private String getEventsFromAnXMLFile(Activity activity)     throws XmlPullParserException, IOException     {        StringBuffer sb = new StringBuffer();        Resources res = activity.getResources();        XmlResourceParser xpp = res.getXml(R.xml.test);                xpp.next();        int eventType = xpp.getEventType();         while (eventType != XmlPullParser.END_DOCUMENT)          {             if(eventType == XmlPullParser.START_DOCUMENT)              {                sb.append("******Start document");             }              else if(eventType == XmlPullParser.START_TAG)              {                sb.append("\nStart tag "+xpp.getName());             }              else if(eventType == XmlPullParser.END_TAG)              {                sb.append("\nEnd tag "+xpp.getName());             }              else if(eventType == XmlPullParser.TEXT)              {                         sb.append("\nText "+xpp.getText());             }             eventType = xpp.next();         }//eof-while         sb.append("\n******End document");         return sb.toString();     }//eof-function 

The entire XML document is traversed and the node name and content are output.

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.