JDOM

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.

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

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
bharadwaja
bharadwaja
5 years ago

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

Back to top button