File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating XML

 
Paul Keohan
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic