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:


package com.javacodegeeks.java.core;

import java.io.FileWriter;
import java.io.IOException;
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.

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.

Inline Feedbacks
View all comments
Back to top button