I have an application where at one stage, I need to download the latest xml file from a server and store it in internal storage (data/data/packagename/filename.xml). I've searched online and its not possible to save file at runtime to res folder, so I have to use internal storage.
However, I'm struggling to try and parse the xml file. Previously I've been using the XmlResourceParser to parse static xml already stored in res/xml/ folder. It's very easy to use. So I'm wondering if theres a way to use XmlResourceParser to parse the newly downloaded xml file. Otherwise, how should I parse the xml file?
Thanks.
XmlResourceParser is for binary xml files in res folder only. But you can use XmlPullParser. It can read data from an InputStream.
Related
I would like to port some code to Android(more pecisely an XML parser net.xqhs.util.XML ) in order to do that I need to read files from assets or raw folder using RandomAccessFile.
How can I do this?
Thank you!
That is not possible, sorry. Neither an asset nor a resource is a file on the Android device, and so while you can get an InputStream on them, you cannot create a RandomAccessFile on them.
Your choices are:
Use any of the available XML parsers for Android that take an InputStream. There are three XML parsers that are part of Android (DOM, SAX, XmlPullParser) that can work with streams. I would expect most of the third-party XML parsers to also support InputStream.
Copy the asset or raw resource (e.g., using an InputStream) to a local file. Then, create a RandomAccessFile for that file.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
where to place an xml file with data in app
I'm kinda new to android dev. I'm storing the data related to my app in an XML file. I'm trying to use SAX parser to read/write/edit data to that xml file.
Where is the proper place to store this xml file? In assets/res/some-other folder ? (I want to use internal memory)
Is there any problem with saving app data to internal memory? This app won't have huge amounts of data. I need to read/ write/ modify this xml file depending on user actions. (I read that we can't edit files in assests at runtime !!)
Given a particular directory, how can I access it using InputStream in order to feed it to SAX XMLReader?
If you want also to write the file, I don't think that you can do that in /res or /assets. Take a look at the android data storage options. For example, you can write the XML file to the internal storage.
http://developer.android.com/guide/topics/data/data-storage.html
Yes, you can store your xml files in assets.
But I suggest you to create a folder name XML in /res/. It'll help you to easily use it with id "R.xml.xml_file".
Though it depends on entirely your requirements, I would suggest better to create a directory using getDir(String dirName, int mode) and store your xmls here.
Above can be used to store the data dynamically means when application is running. But if your application is already having xml files, its better to store it in asset folder because you can create sub directories easily here.
I need to save a xml file from sdcard to my android app, in res/values. How can I do this? Can anyone help me?
just use the java file command for your xml file on sdcard.
File file=new File("/mnt/sdcard/example.xml");
and using DOM or SAX parser you can parse the data(information) from it for your application.
Thnx.
EDIT: parsing local xml file using Sax in android, Read XML Resources in Android, using XmlResourceParser: XML parsing interface or Here or Here Look at these tutorials.
They are just folders in an Android project when you develop. When the app is compiled, everything in src and res is turned into binary code, and packed in one apk. Therefore I don't think that you will have res folder or anything like that in the device.
I'm trying to find the best way to store XML files from the internet, so that i can parse them at a later date. I have no problems with downloading an xml file, and storing it in the
/data/data/package_name/files
folder. However i'm not sure how to read this file after it has been saved there, and to pass that read file to an instance of a SAX Parser. Additionally, i have noticed that it is quite slow to read the file from the phone, which is a concern.
I have read the storing XML files in the res/xml folder is a lot quicker - but i'm not sure if it's possible to save a file from the internet into this folder at runtime?
Any help would be appreciated :)
Cheeers
Store it in the cache directory.
getCacheDir();
so you can say filename = getCacheDir() + file123.xml;
You cant write to the res folder at all. These resources are compiled into your app at build time and are read only. As for reading the file, You should just be able to create an input stream for it in the standard java way and parse out the bytes using your sax parser.
This section of the guide describes how to access the internal and external filesystems and how to get input/output streams.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
I am having a file in the folder res/raw/a.xml.
I want to write some data to this file?
How it can be done in Android?
How can we access a file stored in local directory in order to write data to that file.
can anyone help me in sorting out this issue ?
Thanks in Advance,
Hi, I am having a file in the folder res/raw/a.xml. I want to write some data to this file? How it can be done in Android?
You cannot modify a resource at runtime, sorry.
How can we access a file stored in local directory in order to write data to that file.
Use getFilesDir() to get a File object pointing to your app's local directory. Then, use standard Java I/O.