File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Updating 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 XML" Watch "Updating XML" New topic
Author

Updating XML

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Is there a straightforward way to update an XML file using JDOM or DOM? Everything I look at seems to imply XSL files (I think) are needed. I want to load a file using the SAXBuilder, change the value of some elements or attributes, and save back the updated version of the XML file. Why is so difficult to get information on how to do this?
Thanks for any help.
Paul
David Patterson
Ranch Hand

Joined: Jul 01, 2002
Posts: 65
If you read in the XML file with SAX, then you don't have any XML internal structure to update. You are free to capture the data via SAX and then create a new document with any new content.
If, however, you read the file with DOM (no expereince with JDOM), you have a structured object with the entire contents. You can update any node, add new nodes, add/change attributes. After you are done, you can use the serializer methods to write it out to a file or stream.
Dave Patterson
PatterD Consulting, Inc.
patterd1@comcast.net
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Paul is using JDOM. org.jdom.input.SAXBuilder is a JDOM class, which uses SAX parser internally. It is fast and easy to use.
Paul, you can use org.jdom.Element, the class has many methods to be used for your purposes. See http://www.jdom.org/docs/apidocs/org/jdom/Element.html
After you finished your modification of the org.jdom.Document, then you can use org.jdom.output.XMLOutputter to save your changes back to the original file or a new file.
Nice and easy! HTH.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating XML