JDOM XML parser example to modify an existing XML file :
- Add a new element
- Update existing element attribute
- Update existing element value
- Delete existing element
Oct 6, 2018 - In this article, we will learn how to update or modify existing XML file in Java using JDOM Parser. In this example, we demonstrate the use of DOM parser to modify an existing XML file: Add a new element; Update existing element attribute; Update existing element value; Delete existing element; 1. See before and after XML file. File: file.xml – Original XML file.
1. XML File
See before and after XML file.
File : file.xml – Original XML file.
Later, update above XML file via JDOM XML Parser.
- Add a new “age” element under staff
- Update the staff attribute id = 2
- Update salary value to 7000
- Delete “firstname” element under staff
File : file.xml – Newly modified XML file.
2. JDOM Example
JDOM parser to update or modify an existing XML file.
About the Author
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.Comments
After you have constructed a DOM (either by parsing an XML file or building it programmatically) you frequently want to save it as XML. This section shows you how to do that using the Xalan transform package.
Using that package, you will create a transformer object to wire a DOMSource to a StreamResult. You will then invoke the transformer's transform() method to write out the DOM as XML data.
Reading the XML
The first step is to create a DOM in memory by parsing an XML file. By now, you should be getting comfortable with the process.
Note:The code discussed in this section is in the file TransformationApp01.java.
Download the XSLT examlles
and unzip them into the install-dir/jaxp-1_4_2-release-date/samples directory.The following code provides a basic template to start from. It is basically the same code as was used at the start of the Document Object Model lesson.
Creating a Transformer
The next step is to create a transformer you can use to transmit the XML to System.out. To begin with, the following import statements are required.
Here, you add a series of classes that should now be forming a standard pattern: an entity (Transformer), the factory to create it (TransformerFactory), and the exceptions that can be generated by each. Because a transformation always has a source and a result, you then import the classes necessary to use a DOM as a source (DOMSource) and an output stream for the result (StreamResult).
Next, add the code to carry out the transformation:
Here, you create a transformer object, use the DOM to construct a source object, and use System.out to construct a result object. You then tell the transformer to operate on the source object and output to the result object.
In this case, the 'transformer' is not actually changing anything. In XSLT terminology, you are using the identity transform, which means that the 'transformation' generates a copy of the source, unchanged.
Note:You can specify a variety of output properties for transformer objects, as defined in the W3C specification at http://www.w3.org/TR/xslt#output. For example, to get indented output, you can invoke the following method:
Finally, the following highlighted code catches the new errors that can be generated:
Notes
- TransformerExceptions are thrown by the transformer object.
- TransformerConfigurationExceptions are thrown by the factory.
- To preserve the XML document's DOCTYPE setting, it is also necessary to add the following code:
To find out more about configuring the factory and handling validation errors, see Reading XML Data into a DOM.
Running the TransformationApp01 Sample
- Navigate to the samples directory.
Download the XSLT examples by clicking this link
and unzip them into the install-dir/jaxp-1_4_2-release-date/samples directory.- Navigate to the xslt directory.
- Compile the TransformationApp01 sample.Type the following command:
- Run the TransformationApp01 sample on an XML file.In the case below, TransformationApp01 is run on the file foo.xml, found in the xslt/data directory after you have unzipped the samples bundle.You will see the following output:As mentioned in Creating a Transformer, this transformer has not actually changed anything, but rather has just performed the identity transform, to generate a copy of the source. A real transformation will be performed in Generating XML from an Arbitrary Data Structure.
Writing Out a Subtree of the DOM
It is also possible to operate on a subtree of a DOM. In this section, you will experiment with that option.
Note:The code discussed in this section is in TranformationApp02.java. If you have not done so already,
download the XSLT examples
and unzip them into the install-dir/jaxp-1_4_2-release-date/samples directory.The only difference in the process is that now you will create a DOMSource using a node in the DOM, rather than the entire DOM. The first step is to import the classes you need to get the node you want, as shown in the following highlighted code:
The next step is to find a good node for the experiment. The following highlighted code selects the first <name> element.
In Creating a Transformer, the source object was constructed from the entire document by the following line of code
However, the highlighted line of code below constructs a source object that consists of the subtree rooted at a particular node.
Running the TranformationApp02 Sample
- Navigate to the samples directory.
Download the XSLT examples by clicking this link
and unzip them into the install-dir/jaxp-1_4_2-release-date/samples directory.- Navigate to the xslt directory.
- Compile the TranformationApp02 sample.Type the following command:
- Run the TranformationApp02 sample on an XML file.In the case below, TranformationApp02 is run on the file foo.xml, found in the xslt/data directory after you have unzipped the samples bundle.You will see the following output:This time, only the first <name> element was printed out.At this point, you have seen how to use a transformer to write out a DOM and how to use a subtree of a DOM as the source object in a transformation. In the next section, you will see how to use a transformer to create XML from any data structure you are capable of parsing.