where to store XML files - android

I'm storing app data in a few XML files. Should I put these XML files in the 'res' folder? Is there anything special I need to do besides just using something like:
XmlPullParser xpp = getResources().getXml(R.xml.myXMLfile);
Thanks

I would suggest to store these file in the assets folder. Use the following code to read file form the assets folder:
getResources().getAssets().open(fileName);

No, nothing, as long as these are completely static in nature.

If it is an app data & if you feel to have lightweight app then try to store in the internal memory ,make use of it & get benefited.

Related

Add XML Serialized object to APK so it is copied into the app's private files directory

I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);

Distribute an xml file with my Android app

I have created a java application using "Simple" that allows me to serialize objects in some configuration xml file.
These files must be redistributed with my android application, but here comes the problem:
Where do I place the files? Is it better to place them in the res/xml folder or create a custom path to the assets folder (assets/myfiles)?
In the examples given with Simple if I want to deserialize an object I need an xml file:
File fl = new File ("string to file path");
but if the file is in the resources of my project, how do I get the file?
Searching the internet I found only examples that use a XmlResourceParser but I don't know how to use it with Simple!
If you are going to include files you can later access from your application you'll need to put them in the assets folder. After your application has been installed and running you can get get that xml file by using the AssetManager class:
AssetManager assets = getAssets();
InputStream stream = asset.open("filename");
That will give you an InputStream for you to do whatever you need.

Where do I put (and how do I read) my own custom XML in Android?

Sorry, this seems like such an obvious question, but I just can't find an answer in the docs...
I have my own XML doc (not Android related) that I need to pull and parse at run time.
Where do I store it and what's the best way to retrieve it? Do I just read it as a text document into an XML parser? Or does Android come prepared to hand me back a parsed XML object from a well formed xml?
TIA
Put it in a xml folder in res.
You can put it in assets folder of your project.
For parsing you can use openXmlResourceParser(filename) method of AssetsManager class
XmlResourceParser parser = getResources().getAssets().openXmlResourceParser (String fileName);
Best way is to put your xml in assets folder and read xml from assets.
Read xml from assets

How to create new XML resource ids at runtime?

I want to create new XML resource IDs at runtime. Just generic XML resources like in the res/xml folder, but from a file on an SD card or anywhere in the filesystem.
I found this solution that says it's possible: How do I generate XML resources at runtime on Android?.
It says to:
Save XML into another external folder (SD card or so)
Create AssetManager over those file(s)
Then create Resources which can be later used as ordinary Android resource stored in APK
I don't understand step 2 since there is no constructor for AssetManager. How do I create an AssetManager for external files?
Thanks in advance,
Barry
By create, I think they mean get the AssetManager:
Context.getAssets()
Then, use the asset manager to open the XML file:
AssetManager.openXmlResourceParser(String fileName)

access resources of an apk

I'm thinking about putting a .txt file in res/drawable or in res/layout to have it in the apk when the application is packed, and then after install to open and read from it to perform some tasks. Can this be done? If yes please let me know how can I access the file, as I don't know it's path.
A .txt file is neither a drawable nor a layout. You should put it in res/raw. You can get access to it like:
Resources resources = this.getResources();
InputStream is = resources.openRawResource(R.raw.yourtextfile);
Try/catch and errorhandling omited in above code.

Categories

Resources