Reading XML file from resources

I have an XML file that I need to parse in the Android SDK.

How can I read the path to the XML file from resources?

XML contains:

<Book>
<Chapter>
<NO>   1   </NO>
<Text>  My Lord </Text>
</Chapter> 

<Chapter>
<NO>   1   </NO>
<Text>  My Lord </Text>
</Chapter>
</Book>
+5
source share
3 answers

Put it in a folder your_project_root\res\xml\. Then you can open it with:

Resources res = activity.getResources();
XmlResourceParser xrp = res.getXml(R.xml.your_resId);

Below is a usage example XmlResourceParserhere:

http://android-er.blogspot.com/2010/04/read-xml-resources-in-android-using.html

+18
source

If you have an XML file in the source folder in your resources, you can read it using the following code:

Context context = getApplicationContext();
InputStream istream = context.getResources().openRawResource(R.raw.test);

I hope this is useful to you.

+4
source

xml xml . .

try {
            XmlPullParser xpp=getResources().getXml(R.xml.words);

            while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
                if (xpp.getEventType()==XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("word")) {
                        items.add(xpp.getAttributeValue(0));
                    }
                }

                xpp.next();
            }
        }
        catch (Throwable t) {
            Toast
                .makeText(this, "Request failed: "+t.toString(), Toast.LENGTH_LONG)
                .show();
        }
+1

All Articles