Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and where to add custom xml in my android app?

Tags:

android

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?

like image 334
piks Avatar asked Feb 22 '12 05:02

piks


People also ask

Where is XML used in Android?

XML stands for eXtensible Markup Language, which is a way of describing data using a text-based document. Because XML is extensible and very flexible, it's used for many different things, including defining the UI layout of Android apps.

Where is the XML file in Android Studio?

You will find out this file inside the res folder and inside it there is another folder named layout where you will get all the layout files for their respective activities or fragments.

Does Android apps use XML?

eXtensible Markup Language, or XML: A markup language created as a standard way to encode data in internet-based applications. Android applications use XML to create layout files. Unlike HTML, XML is case-sensitive, requires each tag be closed, and preserves whitespace.

How is XML used in Android?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.


2 Answers

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();
    }
    
like image 92
Oleksii Masnyi Avatar answered Oct 04 '22 08:10

Oleksii Masnyi


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?

like image 35
Dharmendra Barad Avatar answered Oct 04 '22 07:10

Dharmendra Barad