Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating an Existing xml

 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
Actually I want to modify an existing value with the value which I'm getting from database.. Now the point is.. I'm trying to read an exisiting xml file write it to a new xml with new value in it.. But i'm unable to form a way through which I can read a xml as well as write an xml same time with some of the values changed.. can any one provide me the code....
It will be a great help.......
and it's urgent too.............
thanks in advance...
Akshay
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's start with whether you're reading the XML into a DOM tree or using a custom SAX handler?
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Dom parser for reading the xml.. can u please suggest me some way to achieve this.. as I've a deadline... and I'm totally new to XML...
thanks in advance
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Once you've got the org.w3c.dom.Document, you need to locate the node you wish to update. In your case, it seems that you only want to change the data of an existing Text node into something else, right? If that's all you want to do, then you can do it with org.w3c.dom.Text#setData("the new data"), which makes the change to your in-memory DOM tree. After this, you need to write the DOM tree into a file again. For that, javaalmanac.com has an example.
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...
One more advice help needed I've a method which takes four parameters
changeValue(String elem1,String elem2,String elem3,String value) then according to the parameter's I have to check whether which node user has to modify and then change value of the node accordingly in the Document..
can u suggest..
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm. What do the first three parameters mean? I assume they somehow tell you which node's value you need to modify. Once you have a Document object, you can get the root element with Document#getDocumentElement(), and from there on you can use Element#getElementsByTagName() to navigate to the node you wish to modify. It's all in the API...
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot...
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u send me a sample code to find to a particular node and to change it's value.In whatever heirarchy it may be.......it will be a gr8 help...
waiting for ur reply......
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also can u pls tell what all packages I need to import for the example u told me to write a document.. for ex.. where can i find Source,Result,DOMSerializer and other classes used in that code..
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example I linked to uses
  org.w3c.dom.Document
  javax.xml.transform.Result
  javax.xml.transform.Source
  javax.xml.transform.dom.DOMSource
  javax.xml.transform.stream.StreamResult
  javax.xml.transform.Transformer
  javax.xml.transform.TransformerFactory
  javax.xml.transform.TransformerConfigurationException
  javax.xml.transform.TransformerException
DOMSerializer is a Xalan-specific class (org.apache.xalan.serialize.DOMSerializer), which you would have easily learned yourself by typing "DOMSerializer" into Google...
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Lasse !!
your advice and example helped me a lot...I was able to do my task smoothly...
thank you very much..
Apart from the example you gave there are few good tutorials from which contributed in the same...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic