0

i have an xml called weatherdata.xml (which resides in my eclipse's >assets< folder) and it looks like

<?xml version="1.0" encoding="utf-8"?>
<Weather>
<weatherdata>
    <id>1</id>
    <city>Berlin</city>
    <tempc>0°C</tempc>
    <tempf>32°F</tempf>
    <condition>Snowing</condition>
    <windspeed>5 kmph</windspeed>
    <icon>snowing</icon>
</weatherdata>
    <weatherdata>
    <id>5</id>
    <city>Sydney</city>
    <tempc>32°C</tempc>
    <tempf>89.6°F</tempf>
    <condition>Sunny</condition>
    <windspeed>10 kmph</windspeed>
    <icon>sunny</icon>
</weatherdata>

so i was trying to add one more city, at runtime .. i tried with java alone and was working fine with this

i thought it would work fine with android, but as android functions altogether differently from a desktop application so couldn't go further

and i found this interesting, (though it was not appending)

so my question is

  • what this sdcard, do i need to write to it

  • so if im writing to sdcard can i expect the same output on emulator aswell as on actual device

  • if yes,will this be the path- /sdcard/weatherdata.xml

apart from that, is there any rights(manifest.xml) bothering me to write to an xml file

Community
  • 1
  • 1
x-code
  • 608
  • 1
  • 10
  • 30
  • found something here http://androidideasblog.blogspot.in/2010/01/read-write-and-parse-xml-file-in.html – x-code May 14 '13 at 10:17

3 Answers3

1

Assets are read-only files packaged as part of the APK. You cannot change them.

You have 2 places where you can write files for your application:

  1. SD-card (or "external storage")
  2. Private data area for your application on the phone's internal memory

These are a bit different and have a different set of advantages/disadvantages, as follows:

  1. SD-card:

    • Files you write on the SD-card will not be deleted when your app is uninstalled
    • You need READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions to access files on the SD-card
    • Some devices don't have SD-card support or the user may not have installed an SD-card in the slot
    • The user can remove/replace/format the SD-card in which case any files you write there are lost
    • User can read/write any files on the SD-card by connecting his device to a PC or by removing the SD-card and mounting it on his PC
  2. Private data area:

    • Files you write here will be deleted when your app is uninstalled
    • User cannot read or write these files (unless his phone is rooted)
    • You don't need any permissions to read/write in the private data area

I would suggest that you copy your XML file from the assets to the private data area (if it isn't already there) and then always use the copy from the private data area. If you want to add entries to it, then you update the copy in the private data area.

To open a file in the private data area, use openFileInput() and openFileOutput(). The behaviour is the same on the emulator as it is on a device.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • nice info, seems this (private data area) a new jargon, so any tips on using this.. – x-code May 14 '13 at 10:12
  • Just search for examples with `openFileInput()` or `openFileOutput()` – David Wasser May 14 '13 at 10:26
  • hmm..found something here http://androidideasblog.blogspot.in/2010/01/read-write-and-parse-xml-file-in.html, hope this works, anyway thanks dude... – x-code May 14 '13 at 10:31
  • See `copyDataBase()` method in [this article](http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/). It explains how to read a file from assets and write to private data area. – David Wasser May 14 '13 at 17:22
0

1) sdcard is where you will write your file (note that the xml you have in assets folder in eclipse wont be there, it will be bundled with your app)

2) Yes, the behaviour should be the same on the emulator and on a physical device

3) You can choose the name you want for your file, but note that it wont be the same file that you put in your assets (see 1). Also, you should be using Environment.getExternalStorageDirectory(); rather than hardcoding the directory.

You will need to add this to your manifest :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
dqms
  • 303
  • 1
  • 10
  • (for ur third answer) so how would i retrieve my new entry(city) the next time i re-launch my application... – x-code May 14 '13 at 10:06
0

This's how i got

 try {
        File file = getBaseContext().getFileStreamPath("weather.xml");
        if (file.exists()) 
        {
            Log.d("weather.xml", "File Found !!!");
            FileOutputStream fOut = getApplicationContext().openFileOutput(
                    "weather.xml", MODE_APPEND);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            String fileContent = "<Weather><weatherdata><city>Versace</city></weatherdata></Weather>"; 
            osw.write(fileContent);
            osw.flush();
            osw.close();
            Log.d("write mode: ", "MODE_APPEND");
        }
        else
        {
                FileOutputStream fOut = getApplicationContext().openFileOutput(
                "weather.xml", MODE_PRIVATE);
                OutputStreamWriter osw = new OutputStreamWriter(fOut);
                String fileContent = "<?xml version="1.0" encoding="utf-8"?><Weather><weatherdata><city>Berlin</city></weatherdata></Weather>"; 

                osw.write(fileContent);
                osw.flush();
                osw.close();
                Log.d("write mode: ", "MODE_PRIVATE");
        }
        Log.d("status", "Finished writing to XML");
    } catch (FileNotFoundException e) { Log.e("Error", "XML writing exception");
    } catch (IOException e) { e.printStackTrace(); }

Here point of interest is with write modes, MODE_APPEND-is for appending an already existing xml file and MODE_PRIVATE, is for creating a new file, and write to it. Lastly getBaseContext().getFileStreamPath(), used to check the target file exists or not..

x-code
  • 608
  • 1
  • 10
  • 30
  • this can still be done in better ways, though, and by the way David's explanation, helped me to understand the concept of storage.. – x-code May 14 '13 at 16:18