Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add elements to already created XML

 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am new to XML.

Right now i am able to read or parse XML with help of my java code.
I have my XML file as follows:



Now i want to update this xml with help of my java code, like with help of my java code i can add more <text></text> element into xml.

Thanks
Gaurav
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reading/parsing the xml, you probably used an implementation of SAX.

SAX cant update the xml that you have loaded into memory using java.

You can use DOM to both read/parse xml, update it, then write it out to an output destination like a file.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Sir can you please give me some small working example that how to add data to XML at runtime.

Thanks
Gaurav
 
aschwin versteegden
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DO You maybe have an example of the usage of DOM parsing mechanism
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of how to read an XML file and create a DOM Document from it can be found here.

Here is an example of how to add elements to a DOM Document.

And finally, here is an example of how to write a DOM Document to a file.

Putting these pieces together you shoudl be able to read an XML file into a DOM Document, modify it, and then write it back to a file.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot all.

My problem is solved with all your helps.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic