DOM provides many handy classes to create XML file easily. Firstly, you have to create a Document with DocumentBuilder class, define all the XML content – node, attribute with Element class. In last, use Transformer class to output the entire XML content to stream output, typically a File.

In this tutorial, we show you how to use DOM XML parser to create a XML file.

DOM Parser Example

At the end of the example, following XML file named “file.xml” will be created.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
	<staff id="1">
		<lastname>mook kim</lastname>

File : – Java class to create a XML file.

package com.mkyong.core;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class WriteXMLFile {
	public static void main(String argv[]) {
	  try {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("company");
		// staff elements
		Element staff = doc.createElement("Staff");
		// set attribute to staff element
		Attr attr = doc.createAttribute("id");
		// shorten way
		// staff.setAttribute("id", "1");
		// firstname elements
		Element firstname = doc.createElement("firstname");
		// lastname elements
		Element lastname = doc.createElement("lastname");
		lastname.appendChild(doc.createTextNode("mook kim"));
		// nickname elements
		Element nickname = doc.createElement("nickname");
		// salary elements
		Element salary = doc.createElement("salary");
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("C:\\file.xml"));
		// Output to console for testing
		// StreamResult result = new StreamResult(System.out);
		transformer.transform(source, result);
		System.out.println("File saved!");
	  } catch (ParserConfigurationException pce) {
	  } catch (TransformerException tfe) {

A new XML file is created in “C:\\file.xml“, with default UTF-8 encoded.

For debugging, you can change the StreamResult to output the XML content to your console.

StreamResult result =  new StreamResult(System.out);
transformer.transform(source, result);
Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts