JDOM

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.

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.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button