Skip to content Skip to sidebar Skip to footer

What's The Best Way To Store And Access Xml In Android?

While I realize resources themselves are defined in XML files, if I have an XML file of my own type that I wish to use, should I be storing them in 'res/xml'? Is there a better way

Solution 1:

I would go with adding the XML resource inside the res folder with the specific resource type. Its a convention that I have adapted to, having all my files in the same directory for organization.

If you add your XML file inside of res/xml it can be accessed anytime at run-time via Resources.getXML()

Solution 2:

If you prefer ease of coding over lightning fast speed then I would use the Simple XML library instead. It is just easier to program for. I wrote a blog post that you can view that explains how to include it in a project of yours.

Solution 3:

First knowing how to correctly Access Files in Android:

You might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID. Instead, you can save your resources in the assets/ directory.

Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources. Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager.

However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory and read a stream of bytes using openRawResource().

Accessing XML Files

http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml

res/xml/

Arbitrary XML files that can be read at runtime by calling Resources.getXML()

Source

Resourcesres= activity.getResources();
XmlResourceParserxrp= res.getXml(R.xml.the_file_name_aka_resource_ID);

Post a Comment for "What's The Best Way To Store And Access Xml In Android?"