Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Appending Data to XML File

Thread view: 
Emmanuel - 13 Oct 2006 15:09 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.