How to create XML in java (DOM DocumentBuilder)

There are various ways to generate XML files in java. One of the easiest ways is to use DOM DocumentBuilderFactory.

DOM provides easy APIs to create XML. You will have to create DocumentBuilder from DocumentBuilderFactory, then add all the nodes like elements, attributes, comments, text nodes.

Finally you need to transform the document in to xml using transformers(javax.xml.transform.Transformer).You can transform document to xml file or simply console output for your convenience.

The XML we need to create is “rices.xml”, it will look like below at the end:

xml

Java program to create the above XML file:

package com.mksiva.java.core;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;

public class CreateXMLUsingDOM {
public static void main(String args[]){
try {
DocumentBuilderFactory theDocBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder theDocBuilder = theDocBuilderFactory.newDocumentBuilder();
Document theDoc = theDocBuilder.newDocument();
// root element
Element theRootElement = theDoc.createElement("rices");
theDoc.appendChild(theRootElement);

// rice element
Element theRiceEl = theDoc.createElement("rice");
// adding attribute 'id' to 'rice' element
theRootElement.appendChild(theRiceEl);
// setting attribute to element
Attr theAttr = theDoc.createAttribute("id");
theAttr.setValue("1");
theRiceEl.setAttributeNode(theAttr);

 // Adding comment
 Comment theComment = theDoc.createComment("Rice and it's properties");		
 theRootElement.insertBefore(theComment, theRiceEl);

// name element
Element theNameEl = theDoc.createElement("name");
// adding text node
theNameEl.appendChild(theDoc.createTextNode("ponni"));
theRiceEl.appendChild(theNameEl);

// type element
Element theTypeEl = theDoc.createElement("type");
// adding text node
theTypeEl.appendChild(theDoc.createTextNode("medium-grain"));
theRiceEl.appendChild(theTypeEl);

// country element
Element theCountryEl = theDoc.createElement("country");
// adding text node
theCountryEl.appendChild(theDoc.createTextNode("India"));
theRiceEl.appendChild(theCountryEl);

// days element
Element theDaysEl = theDoc.createElement("days");
// adding text node
theDaysEl.appendChild(theDoc.createTextNode("130"));
theRiceEl.appendChild(theDaysEl);

// write the content into xml file
TransformerFactory theTransformerFactory = TransformerFactory.newInstance();
Transformer theTransformer = theTransformerFactory.newTransformer();
theTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
theTransformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource theSource = new DOMSource(theDoc);
StreamResult theResult = new StreamResult(new File("C:\\temp\\rices.xml"));
theTransformer.transform(theSource, theResult);

// Output to console for testing
StreamResult theConsoleResult = new StreamResult(System.out);
theTransformer.transform(theSource, theConsoleResult);

} catch (Exception e) {
e.printStackTrace();
}
}
}

To align the xml or console output to more readable pretty format, the following two options in the transformers are used. This will add 4 space indentation to each tag in the element.

theTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
theTransformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOM – Document Object Model

Leave a Reply

Your email address will not be published. Required fields are marked *