• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending to an XML file  RSS feed

 
V Bose
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to keep adding elements to an XML file each time the user exits the application. Sort of like storing the 'last known good state'.

If I treat the XML file as a simple text file, appending information to the end of a file seems to be expensive in the sense, I have to read the entire file before I can get the file marker to the end and then remove the ending tag, and append stuff and place the ending tag.

Is there an efficient way to do this ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know the size of the end tag, and the size of the file, you might be able to jump directly just before the end tag using a RandomAccessFile.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done something similar by leaving the XML file incomplete - ie without a root element. This makes appending simple at the cost of having to supply starting and ending root elements when you need to parse the file.
Bill
 
srinivas kalluri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't you do the adding operation to the xml file using xml operations itself.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!