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?
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.
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.
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.
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.
It's easy.
\res\xml\
folder.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();
}
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With