Load and modify xml file in Android - android

I have an xml file which will be in either assets or res folder which i need to modify by adding some values and then i need to convert the xml into a string to be sent as part of a soap request. What would be the easiest way to achieve this?

You can't modify resource files.
If you don't need modify resources, you can put the XML file in assets folder. Then you can access it by AssetManager (returns by getResources().getAssets()). With AssetManager you can get InputStream from XML file or use XMLResourceParser. And do with it whatever you want.

Related

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.

Access to resource folder route

I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).
I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?
new FileOutputStream("/res/drawable/xxx.png");
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream form a file in resources,you should consider creating a raw folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.

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