aspose file tools*
The moose likes XML and Related Technologies and the fly likes Can i append data to existing XML file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Can i append data to existing XML file?" Watch "Can i append data to existing XML file?" New topic
Author

Can i append data to existing XML file?

Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Hi all,

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


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
You can NOT have a correctly formed XML file AND append data to the end because a correctly formed XML file has the closing root tag at the end, right?
However, if you just write xml fragments to the file, you can append at the end - I do this all the time. The trick is that when you want to parse it as an XML document, you need to modify the file or feed the parser with separate start and end tags.
See this article for one solution.
Bill
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Thank you William Brogden ,



i read in some article that XML NameSpace is introduced to avoid name collision .which occurs while linking\combining 2 XML files during parsing....from that we can conclude ..we can store data in different files ,while parsing we can treat them as a single xml file.

Can we do same for appending data?

Exact scenario
I am writing an application say discussion form ,similar to JAVARANCH.com ...daily many number of members will post there doubts in the form ..i will accept data from users and store that in XML file say "Subject_Name.xml"..Now when some person post reply to this form then i want to store that reply data in the same file i.e, in "Subject_Name.xml" file. but i should not destroy earlier data ,it should preserve previously entered data ..
So that when i click the link it will extract both doubt and reply message data from "Subject_Name.xml" file and arrange in HTML page as needed...

**************************************************************************
In previous post suggested one idea..i understod that idea..

But ,From the reson why NameSpace is used(to avoid name space collision which will occur while combining different xml files) ...i got one more doubt...can i combine xml files and parse as though it is a single xml file?
[ September 05, 2006: Message edited by: harish thrivile ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
If I understand your application correctly, all of the XML formats in this application are under your control. Therefore you will not need to use namespaces at all. Namespaces are only required when you have a mixture of tag names from various sources as in SOAP messages for example.
Bill
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Thank you William Brogden sir ,for replying

But, here i am not using Namespaces .just i took an example to ask whether we can link say 2 xml files ....and, while parsing i should use some technique which will consider that 2 separate xml file as single xml file

As you have mentioned

Namespaces are only required when you have a mixture of tag names from various sources as in SOAP messages

which implies : i can mixup elements from various source(different xml files)...

So,Can you please tell me how to link various source ...or else if you know any link please tell that to me ..

Thank you.
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Did you read the article pointed out by William? It explains how to handle large XML files in general. It's not possible to link one XML file to another inside the XML file.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
that 2 separate xml file as single xml file

I actually wrote the program discussed in the article to let me combine any number of XML file fragments from various sources while preserving the parser error information.
The application was an online exam where the results were recorded as XML fragments appended to the end of files.
Bill
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Thank you William Brogden ,

i have written one program to resolve XML appending problem..which will delete the End_of_root_element_TAG befor inserting new data to file..

You can NOT have a correctly formed XML file AND append data to the end because a correctly formed XML file has the closing root tag at the end, right?


after appending once i am writng END_OF_ROOT_TAG manually...

LOgic used :

First i will store file contents into a Stringbuffer and remove the END_ROOT_ELEMENT_TAG. Data is stored in file once again. First the buffered data which we have just read and manipulated and then i am appending additional data with the END_OF_ROOT_TAG element.

My doubt is:
can i consider this logic ....i am asking because,here i am copying entire data to buffer then once again i am storing it in file...so if data inside file is more then it takes more time..implies drawback in performance ..

So is there any concept called CURSOR(similar to the one which we have in JDBC,used for moving cursor to desire location)...Through which i can avoid reading entire data from file...instead i can move my cursor to END_OF_ROOT_TAG (because That TAG is creating problem here),....

My Question :Is there any techniqe in file operation which will move CURSOR to desired location...if any one knows please assist me or else if you link please post that here..
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Copying the entire file to memory is obviously going to cause problems as the file gets larger and involves lots of unnecessary work.
Fortunately, you could use java.io.RandomAccessFile to read the last N bytes of the XML file, locate the start of the end element, and start writing bytes at that point to add a record. This will be really fast.

Important: note that you will be reading and writing bytes, not characters.

Bill
 
 
subject: Can i append data to existing XML file?