简体   繁体   中英

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 to do this such as using assets and then loading them as binary to be parsed by another XML library?

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()

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM