aspose file tools*
The moose likes XML and Related Technologies and the fly likes Updating an Existing xml 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 "Updating an Existing xml" Watch "Updating an Existing xml" New topic
Author

Updating an Existing xml

akshay pandit
Ranch Hand

Joined: Jun 15, 2003
Posts: 44
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
Let's start with whether you're reading the XML into a DOM tree or using a custom SAX handler?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
akshay pandit
Ranch Hand

Joined: Jun 15, 2003
Posts: 44
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jun 15, 2003
Posts: 44
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jun 15, 2003
Posts: 44
thanks a lot...
akshay pandit
Ranch Hand

Joined: Jun 15, 2003
Posts: 44
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

Joined: Jun 15, 2003
Posts: 44
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jun 15, 2003
Posts: 44
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating an Existing xml