Update a XML file - android

I have a file that I open and parse and some of the categories can be modified. Im wondering how I can update the original Xml file with the new values.

I assume that the XML file is a normal sequential file. If so, you could try to update just one tag, but it would be easier and safer just to rewrite the entire file.

Related

Parse a local xml file and show parsed data in web-view

I have an xml file which is stored locally in my app and I want to show parsed data from it in web-view.
We need more information regarding what exactly you want to do. It is very unclear. There are two things that you could mean:
The XML file is an XHMTL file, that you want to render and show in a webview.
You want to apply styling to an XML file and display it in the webview, preferably using XSLT.
In the former case, it is pretty straightforward. If it is the latter, check out this answer: How can I transform xml to html on android?
To access XML resources stored in res/xml, call getResources().getXml() from any Activity or other Context. You need to supply to getXml() the ID of the XML to load (R.xml.myfile).
to read your xml add code as shown below
XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml);
//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml);
myxml.next();//Get next parse event
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc.
and to get content of code add code shown below

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

android xml file creation

i m working on a project in which i have to store and read some list elements in xml format i did the prasing part by creating file in res/xml and and using getResources().getXml(R.xml.SAMPLEFILE); it works well.. problem arise when i go for file creation..i m unable to find tutorial which lets u creat or open(if already created) xml in res/xml..
http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html
You cannot add anything to your res folder during runtime. If you want the xml to be stored there you'll have to add it in when you compile. If you need it to be dynamic then you'll have to store the xml file in a different place.
One option you have is private internal storage for your application. Check out this page
for an overview of how to save to and read from this internal storage.

Edit text file programmatically

How can we modify a text file from android, such as
append at the end of the file.
clear data at any intermediate position and also blank lines in it.
Is there any option to delete a particular line from the text file, I read so many tutorials regarding this all have mentioned to create a temporary file and asked to copy the contents from the original file except that particular line, and to replace the original file with this temporary file.
Is there any alternative to do this with in that original file itself without creating a temporary file?
Thanks in Advance.
You can't do the second option programmatically, that's virtually impossible. The first option however is possible. Just use an InputStream Object to load your file, then you can get the text from the InputStream as a String, I believe, and then you can append the data at the end. Now that I think of it, it might be possible, but with a heck of a lot of work, to edit the text at the middle... But that's up to you to figure out...
Pretty much everything that is possible with java's file api's can be done in android
You can use a FileOutputStream, passing true for append and then using the normal output stream operations.
For changing files in a random-access manner, try RandomAccessFile. This allows you to change the current byte you are pointing to. However, if you delete data in this manner it won't automatically cause the file to contract to fill the empty space; you would need to do this manually and then change the length of the file via setLength().

Android Write files

I write file to sdcard using BufferedWriter.After that I want to overwrite header of file,but other data must be without changes( In the header I must add size of file). So I think I must change position where I need write.
How can I do that?What function should I use?
You can use a RandomAccessFile to access a file, and modify it.

Categories

Resources