How to create XML file in Java using DOM
java xml file
dom xml java
xml pretty printing
DOM stands for Document Object Model. A DOM is a standard tree structure to define the XML structure. The two most common types of nodes are element nodes and text nodes.
Example
The example contains few important elements:
- create a Document object using DocumentBuilder and DocumentBuilderFactory classes.
- add elements and attributes to the elements.
- use TransformerFactory and Transformer classes to output the Document object to the console or a file.
package com.admfactory.xml;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XMLCreate {
public static void main(String[] args) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
/** root elements */
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("users");
doc.appendChild(rootElement);
/** user elements */
Element user = doc.createElement("user");
rootElement.appendChild(user);
/** set attribute */
user.setAttribute("id", "100");
/** name element */
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("John Doe"));
user.appendChild(name);
/** email element */
Element email = doc.createElement("email");
email.appendChild(doc.createTextNode("john.doe@example.com"));
user.appendChild(email);
/** write the content into xml file */
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
/** Output to file */
// StreamResult result = new StreamResult(new File("C:\\admfactory\\users.xml"));
/** Output to console */
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
In order to have the pretty printing format activated, you need to setup the indent property and the indent-amount.
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
Note: To output the content to a file follow the comments from the example to comment the console output and activate the file output.
/** Output to file */
// StreamResult result = new StreamResult(new File("C:\\admfactory\\file.xml"));
/** Output to console */
StreamResult result = new StreamResult(System.out);
Here is the console output using pretty printing format.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user id="100">
<name>John Doe</name>
<email>john.doe@example.com</email>
</user>
</users>