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.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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: