[jdom-interest] newbie question: save changes to document

graste graste at gmail.com
Wed Jun 1 04:57:14 PDT 2005


I have a question about saving changes to elements into an existing
document. I retrieve an element using a simple xpath expression. After
that I modify the element and want to write the changes back to the
original document. How can I do this?

// code parts stripped (exception handling etc.)

String xpath = "/path/to/some/element";
Document document = builder.build(new File(pathToFile));
Element elem = (Element) XPath.selectSingleNode(document, xpath); // get
elem.getChild("someElem").setText("someContent"); // do some modifications

// somehow save changes here

XMLOutputter outp = new XMLOutputter(Format.getCompactFormat()); // write
outp.output(document, new FileOutputStream(new File(pathToFile))); // back
to disk

Any suggestions to a JDom newbie? I'm using the latest JDom version and
have no problems reading different XML files.

Thanks in advance.


Steffen Gransow

More information about the jdom-interest mailing list