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.