Modifying existing XML file present on sdcard - android

I have been working on android for about 4 months, i am working on a simple application to download a xml file placed on local server to the sdcard. I have done this part successfully, now i want to edit the data present in that xml. To get a more clear picture here is a sample code of my xml file ...
<seekbar>
<value>
50
</value>
</seekbar>
now am reading the value 50 by xml parsing and updating the value of seekbar as 50. Nowi change the value of seekbar to 100 through GUI. So when i click on save button i want that the value 50 should be replaced by 100 in the xml present in the sdcard. I have learnt about the sdcard permission but i am not getting about how to go for this modifying part.. Will i have to parse the whole xml again...??? Please help guys....

You can load the xml into a DOM Object. modify the value or value node and write the new DOM object back to the file.

Related

Reading and writing specific line from csv file in Kotlin

I wanted to read and write specific lines from a CSV file.
Here is an example of my CSV
ID; Code; Name
1; ABHD; Paul
2; HYDR; Arthur
3; POAJ; Jake
4; PLMH; Georges
Actually I know how to read all the file and put it into a list of lines by myFile.readLines().
But I want to read a specific line like the line with the Code ABHD, and later on modify that specific Line without reading and writing the whole file. Any idea ?
Thanks by advance.
A CSV file is a not a database. You can't write individual elements inside the file. You need to read the entire file and write the entire file. If the file is a reasonable size, you can read the entire file into memory, alter the data you want to change (in memory) and then write the entire file again. If the file is huge, you probably need to make your modifications "on the fly" (ie: read a line, (optionally) change it, write the line to the output file).
There are libraries that you can use to parse the data, as CSV can be complicated to parse (especially if it contains text strings).
See https://www.baeldung.com/kotlin/csv-files for some help or search for "kotlin csv parse"

Android: Populate ListView from an internal XML

I found this tutorial which makes exactly what I need :
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
The problem is that the ListView gets populated from an XML found out on an URL. What I have to change so I can load the list items (Image+Text) from an internal XML ?
What do you mean by "internal" xml?
Is your XML file on the SD card or is it compiled in to your application as a raw resource?
Take a look at these links :
Reading an .xml file from sdcard
Android : Reading XML from local resource (for testing)
The line in your code that you will need to change is :
String xml = parser.getXmlFromUrl(URL); // getting XML from URL

Android: Writing to XML file in Assets

I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)

Fetch data from XML file located in sdcard (Android)

This is my first post here :-). I am developing an application for android in eclipse.
My question is I have put an XML file in sdcard with some elements like id,First name and lastname.
Form my UI i have one textbox and button. User will enter ID in the textbox and on click event of button I should fetch the data based on that ID. and display in TextView.
Any help please??
Thanks in advance. :-)
Here is a sample of XML file which is given to me,
<tblInsurees xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
<ID>1</ID>
<LastName>Family 1 (1-42-167)</LastName>
<OtherNames>FamMem1</OtherNames>
<DOB>01-01-85</DOB>
<PhotoFileName xsi:nil="true" />
</tblInsurees>
- <tblInsurees xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
<ID>2</ID>
<LastName>Family 2 (1-4-16)</LastName>
<OtherNames>FamMem1</OtherNames>
<DOB>01-01-85</DOB>
<PhotoFileName xsi:nil="true" />
</tblInsurees>
- <tblInsurees xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
<ID>3</ID>
<LastName>Family 3 (1-4-16)</LastName>
<OtherNames>FamMem3</OtherNames>
<DOB>01-01-98</DOB>
<PhotoFileName xsi:nil="true" />
</tblInsurees>
SO is it possible that somehow I can pass an ID as a parameter and I can just display the rest of the information like name and dob etc on my screen?
Here is a complete example with source. You just to get the File using
File file = new File(Environment.getExternalStorageDirectory()
+ "your_path/your_xml.xml");
Then do the further processing.
If you need example for different types of XML Parsers you can download complete example from here.

How to set values of views from external file in android

Hi
My question is "Is it possible that I pick up all values from external file that is present in phone memory and put these values in my textviews,Buttons etc. ?"
if yes then how?
Step #1: Read in the file using standard Java file I/O.
Step #2: Parse the data.
Step #3: Call appropriate setters on your widgets, such as setText() to change the caption of a TextView, Button, etc.

Categories

Resources