1

I am a beginner in java and I am building an android app.

I want to have an xml file that has text in it. Whenever the server sends updates, I want to change some lines in that file (what I mean by update is changing some lines in that file by erasing the some part of the text written already and replace by the update)

I know nothing about creating,writing or reading from files. When I searched I found out that Internal storage suits me best.

But I do not know if I have to create an xml file manually in any directory or just use the code bellow to create this file automatically?

    // If this is the first time run,execute one time code
    // create XML Internal store 

    String FILENAME = "My_XML_file";
    try{
        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
    } catch (final IOException e) {
        e.printStackTrace();
    }

Thank you in advance!

user1494142
  • 131
  • 1
  • 2
  • 9
  • You can use Shared Prefrence in android. See my post http://stackoverflow.com/questions/12639899/shared-preferences-in-android/12640072#12640072 – Akhilesh Mani Oct 08 '12 at 07:31

2 Answers2

0

- First give the External Storage permission in the Manifest.xml file.

- You can use JAXP & JAXB, and even CASTOR to handle XML in a better way, but still DOM and SAX are inbuilt into Android.

  • You can use something like this

    String s = "/sdcard/Myfolder/mytext.txt";

    File f = new File(s);

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

The code you have will create a file in internal storage but you need a bit more to create and maintain an XML file easily. I suggest you use the Build in Android DOM Parser (Android developers site docs on XML Parse options)

I found this example which explains how to use the dom parser to build a specific (new) XML file from code. In your context where the output stream in created:

StreamResult result = new StreamResult(new File("C:\\file.xml"));

you might want to use the other constructor based on the output stream you created above

StreamResult result = new StreamResult(fos);

In a similar fashion this DOM library allows you to read from an input stream (which you might get from android openFileInput) using DocumentBuilder.parse()

Elemental
  • 7,365
  • 2
  • 28
  • 33