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.


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

	<employee id="10">
		<department>Human Resources</department>
	    <address>34 Stanley St.©</address>


	<employee id="2">
	    <address>123 Stanley St.</address>



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

			// delete an element

			// update an element's value

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

			XMLOutputter xmlOutput = new XMLOutputter();

			//update the file with nice XML formating  

			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) {
		} catch (JDOMException jdomex) {

Updated testFile.xml:

<?xml version="1.0" encoding="UTF-8"?>
  <employee id="120">
    <phonenumber location="home">9120970912</phonenumber>
  <employee id="2">
    <address>123 Stanley St.</address>

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

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

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 ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


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.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

Hi,canu please update the Code,in showing how to edit both and in a single shot..

Back to top button