JDOM Edit XML File Example

JDOM provides very neat way to manipulate XML files, using JDOM is very easy and the code looks clean and readable. Earlier we saw how to read XML using JDOM and how to write XML using JDOM. Here we will learn how easily we can edit XML files using JDOM.

JDOM Edit XML File

For this tutorial, we have following employees.xml file.

We want to change following for every Employee element in the XML.

  1. Update all name element to block letters.
  2. Append M for Male and F for Female in ID attribute.
  3. Remove gender element
  4. Add new element salary with default value 1000 for each employee

JDOM Example – Edit XML File

Here is the java program for above changes.

Notice the use of namespace for retrieving all the elements. Above program produces following output xml.


That’s all for JDOM example to edit XML file in java.

By admin

Leave a Reply

%d bloggers like this: