Modifying the Data

At this point, we only need to implement code within the saveConfiguration( ) method to update the XML document with the modified member variable values. This can be done completely with the JDOM APIs, using the Document object we loaded and saved a reference to when parsing the XML document, as well as the supplied OutputStream (which, in our example, is actually a FileOutputStream wrapping a file on the filesystem). Once updates are made, we need to update that Document object, and then write the changes out to a file. In other applications, the modified Document could be transformed with XSLT and output as HTML or another markup language, or passed on to another application over a network.

Updating the Configuration Information

All that is left to make our application fully functional is to add code to the saveConfiguration( ) method that takes in an OutputStream as an argument, as this is called by the version that takes a String filename as a parameter. Since we saved a reference to the Document object, this is simply a matter of setting the content of the various elements that are modified through the setContent( ) method available on Element instances. We can first handle the hostname, port, and parserClass elements, which are nested directly within the root element:

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import ...

Get Java and XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.