I am writing java application which will store data in XML file..Each
time when i execute that program ,previous data will be overidden..
Is it possible to append data to XML file,if anybody knows please
assist me
Raanan - 13 Oct 2006 16:22 GMT
Hey,
it will help if you write how are you writing to the file today.
anyway, this is a problem that can be solved (if not elegantly then
with force) and by force I mean that an XML file is a txt file first..
and if you know what you want to write you can always just insert it as
text.
Raanan.
http://www.infoservicesonline.com
> I am writing java application which will store data in XML file..Each
> time when i execute that program ,previous data will be overidden..
> Is it possible to append data to XML file,if anybody knows please
> assist me
Simon Brooke - 13 Oct 2006 18:41 GMT
> I am writing java application which will store data in XML file..Each
> time when i execute that program ,previous data will be overidden..
> Is it possible to append data to XML file,if anybody knows please
> assist me
An XML file is required to have only one top-level element. So no, you
can't just append to it. But you can read the file, append children to the
document element, and write it out again.
Roughly something like
DocumentBuilder p = doSomethingToGetDocBuilder();
Document doc = p.parse( new
InputSource( new URL( FILENAME ).openStream( )));
while ( newData.hasMoreElements())
{
Element elt =
doSomethingWithNewData( newData.nextElement( ));
doc.getDocumentElement().appendChild( elt);
}
domPrinter.print( doc, System.out);

Signature
simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; Our modern industrial economy takes a mountain covered with trees,
;; lakes, running streams and transforms it into a mountain of junk,
;; garbage, slime pits, and debris. -- Edward Abbey
Arne Vajhøj - 15 Oct 2006 03:35 GMT
> I am writing java application which will store data in XML file..Each
> time when i execute that program ,previous data will be overidden..
> Is it possible to append data to XML file,if anybody knows please
> assist me
3 different approaches:
1) read in the entire XML file, append elements and
rewrite the entire XML file
2) keep the XML file invalid by missing the last end tag,
simply append lines to the file, spoof the missing end
tag whenever reading the file
3) use a non portable trick by overwriting the last end tag
with the new elements and an end tag using RandomFileAccess
I have some (not particular great) code illustrating all 3
approaches.
Arne