JDOM Write XML File Example from Object

Filed Under: Java

In last tutorial we learned basics of Java JDOM and how we can read XML file to Object. In this tutorial we will learn about JDOM Write XML File example.

JDOM Write XML File

JDOM Document provides methods to easily create elements and attributes. XMLOutputter class can be used to write the Document to any OutputStream or Writer object.

For this example, we will create a list of Employee object and then write it to XML file.

package com.journaldev.xml;

public class Employee {
    private int id;
    private String name;
    private String gender;
    private int age;
    private String role;
    public Employee(){}
    public Employee(int id, String name, int age, String gender, String role){
    public int getId() {
        return id;
    public void setId(int id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getGender() {
        return gender;
    public void setGender(String gender) {
        this.gender = gender;
    public int getAge() {
        return age;
    public void setAge(int age) {
        this.age = age;
    public String getRole() {
        return role;
    public void setRole(String role) {
        this.role = role;

We will set Employee ID as attribute to Employee element and set the namespace for root element Employees.

package com.journaldev.xml.jdom;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

import com.journaldev.xml.Employee;

public class JDOMXMLWriter {

    public static void main(String[] args) throws IOException {
        List<Employee> empList = new ArrayList<>();
        empList.add(new Employee(1, "Pankaj",25,"Male","Java Developer"));
        empList.add(new Employee(2, "Mona",34,"Female","Manager"));
        empList.add(new Employee(3, "Dave",45,"Male","Support"));
        String fileName = "employees.xml";
        writeFileUsingJDOM(empList, fileName);

    private static void writeFileUsingJDOM(List<Employee> empList, String fileName) throws IOException {
        Document doc = new Document();
        doc.setRootElement(new Element("Employees",  Namespace.getNamespace("https://www.journaldev.com/employees")));
        for(Employee emp : empList){
            Element employee = new Element("Employee");
            employee.addContent(new Element("age").setText(""+emp.getAge()));
            employee.addContent(new Element("name").setText(emp.getName()));
            employee.addContent(new Element("gender").setText(emp.getGender()));
            employee.addContent(new Element("role").setText(emp.getRole()));
        //JDOM document is ready now, lets write it to file now
        XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
        //output xml to console for debugging
        //xmlOutputter.output(doc, System.out);
        xmlOutputter.output(doc, new FileOutputStream(fileName));


When we run above JDOM Write XML File example program, we get following XML file.

<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns="https://www.journaldev.com/employees">
  <Employee xmlns="" id="1">
    <role>Java Developer</role>
  <Employee xmlns="" id="2">
  <Employee xmlns="" id="3">

Notice that Employee elements have empty namespace because we set the namespace for root element only. If you want same namespace for all the elements, you need to create them specifically by using the constructor with namespace argument.

Also check the use of Format class for pretty printing XML file. We can use it to output XML in compact and raw format also.


  1. danial says:

    The totorial is very good indeed, couild you please help a little bit more.
    How can I add grand child(grand grand child) node in the XML
    many thanks in advance.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors