10

I want to add one xml file (which has some static data related to my application) to my Android app. The important thing is that we can't add the data to the string.xml file.

At the time the application is launched, it should load all static data from the xml file to my data structure.

Problems:
1. How do I add xml to my Android app using Eclipse?
2. Where should I add my custom xml? (inside the values folder OR res folder OR anywhere else?)
3. As per my understanding, we have to write an xml parser class for this, is it correct or is there any other way to automatically get the parsed xml value in Android?

piks
  • 1,621
  • 8
  • 32
  • 59

2 Answers2

17

It's easy.

  1. Put your xml file into \res\xml\ folder.
  2. Use XmlResourceParser to parse xml data. See the following code

    XmlResourceParser xrp = context.getResources().getXml(R.xml.your_file);
    
    xrp.next();
    int eventType = xrp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG
                && xrp.getName().equalsIgnoreCase("your_target_tag")) {
            String attrValue = xrp.getAttributeValue(null,
                    "attribute");
            int intValue = xrp.getAttributeIntValue(null, "order", 0);
            break;
        }
        eventType = xrp.next();
    }
    
Oleksii Masnyi
  • 2,922
  • 2
  • 22
  • 27
3

It is advisable to create a raw folder in res folder for example. res/raw/yourfile.xml. for storing your custom xml file. And then use XML PullParser technique to parse data. see the below link for more details.

Store static data in Android - custom resource?

Community
  • 1
  • 1
Dharmendra Barad
  • 949
  • 6
  • 14
  • where to add two custom xml one will be having english text and other one will be having french text. – piks Feb 23 '12 at 11:12