tags:

views:

66

answers:

2

i have a xml file named as default xml with sample contents as

<check>hi</check>

in my application i am doing an operation such that it wil operate on the xml and writes the new value to it, by overriding the newvalue on the oldone ex:

<check>hi updated</check>

and i closed my application now

my problem is here:

whenever i start application again i should get the contents of default xml as

<check>hi<check>

instead of

<check>hi updated</check>

how can i achieve this ,can i have an steps to implement this r any sample code to implement this

i wil b thankful to ur valuable replies

+5  A: 

Um... don't overwrite the file then? Keep the changes in memory or work on a temporary copy.

Michael Borgwardt
Too easy! MUST be hard!!
Thorbjørn Ravn Andersen
A: 

Writing data:

FileOutputStream os = new FileOutputStream("C:/cust.xml");
XMLEncoder encoder = new XMLEncoder(os);
Person p = new Person();
p.setFirstName("John");
encoder.writeObject(p);
encoder.close();

Reading data:

FileInputStream os = new FileInputStream("C:/cust.xml");
XMLDecoder decoder = new XMLDecoder(os);
Person p = (Person)decoder.readObject();
decoder.close();

EDIT: You can also use XStream library. Here will you find a complete tutorial about it

nandokakimoto