How to write xml document using dom4j library

In this tutorial you will learn how to write the xml document using dom4j library. The dom4j library provides the good way to write xml document. You can see in below example that first part to writing out an XML document is to have in memory representation of it.

After getting the memory representation of xml document you can create the elements with child elements, you can add the attributes and text data to any level of elements.

Once you have an in memory representation of the XML document, you can serialize it to the file system using FileOutputStream.

You can format the xml in two ways to get the output.

First is crate the OutputFormat by calling OutputFormat.createPrettyPrint();

// Create the pretty print of xml document.
OutputFormat format = OutputFormat.createPrettyPrint();

Second is crate the OutputFormat by calling OutputFormat.createPrettyPrint();

// Create the compact format of xml document.
OutputFormat format = OutputFormat.createCompactFormat();

First format will format in pretty way and second format will simply write the xml in file.

After that you can print the xml document using XMLWrite by passing the outstream and format objects.

Below is complete example of code which writes the xml file with two sub elements.

package com.java.connect.dom4j;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
 
public class XMLWriteExample {
     
    public static void main(String[] args) throws IOException {
        Document document = DocumentFactory.getInstance().createDocument();
        // Create the root element of xml file
        Element root = document.addElement("person");
        // Add some attributes to root element.
        root.addAttribute("attributeName", "attributeValue");
        // Add the element name in root element.
        Element name = root.addElement("name");
        name.addText("Mahendra");
        // Add the element age in root element.
        Element age = root.addElement("age");
        age.addText("29");
        // Create a file named as person.xml
        FileOutputStream fos = new FileOutputStream("person.xml");
        // Create the pretty print of xml document.
        OutputFormat format = OutputFormat.createCompactFormat();
        // Create the xml writer by passing outputstream and format
        XMLWriter writer = new XMLWriter(fos, format);
        // Write to the xml document
        writer.write(document);
        // Flush after done
        writer.flush();
    }
}

The xml document with pretty print will looks like.

<?xml version="1.0" encoding="UTF-8"?>
<person attributeName="attributeValue">
      <name>Mahendra</name>
      <age>29</age>
</person>

And the xml document with compact format will looks like.

<?xml version="1.0" encoding="UTF-8"?>
<person attributeName="attributeValue"><name>Mahendra</name><age>29</age></person>