Home » Core Java » xml » JDOM » Modify XML File in Java using JDOM parser example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Modify XML File in Java using JDOM parser example

In the previous tutorial we show how to read an XML File using JDOM parser. In this tutorial we are going to see how to modify the contents of an XML File.
 
 
 
 
 
 
 
 
 
 
This is the XML File we are going to use for the demonstration.

testFile.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><company>

	<employee id="10">
		<firstname>Jeremy</firstname>
		<lastname>Harley</lastname>
        <email>james@example.org</email>
		<department>Human Resources</department>
		<salary>2000000</salary>
	    <address>34 Stanley St.©</address>

	</employee>

	<employee id="2">
		<firstname>John</firstname>
		<lastname>May</lastname>
		<email>john@example.org</email>
		<department>Logistics</department>
		<salary>400</salary>
	    <address>123 Stanley St.</address>
	</employee>

</company>

ModifyXMLFileWithJDOM.java:

package com.javacodegeeks.java.core;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class ModifyXMLFileWithJDOM {

	private static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\testFile.xml";

	public static void main(String[] args) {

		SAXBuilder saxBuilder = new SAXBuilder();
		File xmlFile = new File(xmlFilePath);

		try {
			Document document = (Document) saxBuilder.build(xmlFile);

			Element rootElement = document.getRootElement();

			Element employee = rootElement.getChild("employee");

			// update the value of an attribute
			employee.getAttribute("id").setValue("120");

			// delete an element
			employee.removeChild("address");

			// update an element's value
			employee.getChild("department").setText("Logistics");

			// add a new element
			Element phoneNo = new Element("phonenumber").setText("9120970912");
			phoneNo.setAttribute("location", "home");
			employee.addContent(phoneNo);

			XMLOutputter xmlOutput = new XMLOutputter();

			//update the file with nice XML formating  
			xmlOutput.setFormat(Format.getPrettyFormat());

			xmlOutput.output(document, new FileWriter(xmlFilePath));

			//you can use this to output the result in standard output for debugging purposeses
			// xmlOutput.output(doc, System.out);

			System.out.println("XML File successfully updated!");

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
	}
}

Updated testFile.xml:

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <employee id="120">
    <firstname>Jeremy</firstname>
    <lastname>Harley</lastname>
    <email>james@example.org</email>
    <department>Logistics</department>
    <salary>2000000</salary>
    <phonenumber location="home">9120970912</phonenumber>
  </employee>
  <employee id="2">
    <firstname>John</firstname>
    <lastname>May</lastname>
    <email>john@example.org</email>
    <department>Logistics</department>
    <salary>400</salary>
    <address>123 Stanley St.</address>
  </employee>
</company>

 
This was an example on how to modify the contents of an XML File using JDOM.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close