Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. 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.

Create XML File in Java using JDOM parser example

In this tutorial we are going to see how to create an XML File in Java using JDOM parser. If you’ve read the previous tutorial on JDOM parser about modifying an XML File you might have a very clear aspect on what this tutorial is all about.

Basically we are going to create a root Element and some new child Elements and and use some of the functions and methods we use on the modifying tutorial in order to create the XML File.

1. Code

Let’s see the code:



import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class CreateXMLFileJDOM {

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

	public static void main(String[] args) {

		try {

			Element company = new Element("company");

			Document document = new Document(company);

			// you might not need this..
			// the firt Element that is created 
			// will be automatically set as the root element
			// document.setRootElement(company);

			Element employee = new Element("employee");

			employee.setAttribute(new Attribute("id", "10"));
			employee.addContent(new Element("firstname").setText("Jack"));
			employee.addContent(new Element("lastname").setText("Johnson"));
			employee.addContent(new Element("department").setText("Logistics"));
			employee.addContent(new Element("age").setText("32"));


			Element employee1 = new Element("employee");
			employee1.setAttribute(new Attribute("id", "2"));
			employee1.addContent(new Element("firstname").setText("John"));
			employee1.addContent(new Element("lastname").setText("Filis"));
			employee1.addContent(new Element("department").setText("Human Resources"));
			employee1.addContent(new Element("age").setText("28"));


			XMLOutputter xmlOutputer = new XMLOutputter();

			// you can use this tou output the XML content to
			// the standard output for debugging purposes 
			// new XMLOutputter().output(doc, System.out);

			// write the XML File with a nice formating and alignment
			xmlOutputer.output(document, new FileWriter(xmlFilePath));

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

		} catch (IOException ex) {

2. Created XML File


<?xml version="1.0" encoding="UTF-8"?>
  <employee id="10">
  <employee id="2">
    <department>Human Resources</department>

This was an example on how to create XML File in Java using JDOM parser.

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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below: