This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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. firstname.lastname@example.org
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.