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:

 

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"));

			document.getRootElement().addContent(employee);

			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"));

			document.getRootElement().addContent(employee1);

			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.setFormat(Format.getPrettyFormat());
			xmlOutputer.output(document, new FileWriter(xmlFilePath));

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

		} catch (IOException ex) {
			System.out.println(ex.getMessage());
		}
	}
}

2. Created XML File

newXMLfile.xml:

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <employee id="10">
    <firstname>Jack</firstname>
    <lastname>Johnson</lastname>
    <department>Logistics</department>
    <age>32</age>
  </employee>
  <employee id="2">
    <firstname>John</firstname>
    <lastname>Filis</lastname>
    <department>Human Resources</department>
    <age>28</age>
  </employee>
</company>

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

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.