How to create XML file in Java using DOM Parser? | Java XML Tutorial


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.

At the end of the tutorial, following XML file named users.xml will be created.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <users>	<user uid="1">		<firstname>Interview</firstname>		<lastname>Bubble</lastname>		<email></email>	</user></users>


Basic steps, in order to create an XML File with a DOM Parser, are:

  • Create a DocumentBuilder instance.
  • Create a Document from the above DocumentBuilder.
  • Create the elements you want using the Element class and its appendChild method.
  • Create a new Transformer instance and a new DOMSource instance.
  • Create a new StreamResult to the output stream you want to use.
  • Use transform method to write the DOM object to the output stream.


package com.example.TestApp;import;import;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;import org.w3c.dom.Document;import org.w3c.dom.Element;public class CreateXMLFileJava { public static void main(String[] args) throws ParserConfigurationException,                                                IOException,                                                TransformerException    {     // 1.Create a DocumentBuilder instance    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();    DocumentBuilder dbuilder = dbFactory.newDocumentBuilder();    // 2. Create a Document from the above DocumentBuilder.    Document document = dbuilder.newDocument();    // 3. Create the elements you want using the Element class and its appendChild method.      // root element    Element users = document.createElement("users");    document.appendChild(users);       // child element    Element user = document.createElement("user");    users.appendChild(user);      // Attribute of child element    user.setAttribute("uid", "1");       // firstname Element    Element firstName = document.createElement("firstName");    firstName.appendChild(document.createTextNode("Interview"));    user.appendChild(firstName);       // lastName element    Element lastName = document.createElement("lastName");    lastName.appendChild(document.createTextNode("Bubble"));    user.appendChild(lastName);       // email element    Element email = document.createElement("email");    email.appendChild(document.createTextNode(""));    user.appendChild(email);       // write content into xml file       // 4. Create a new Transformer instance and a new DOMSource instance.   TransformerFactory transformerFactory = TransformerFactory.newInstance();   Transformer transformer = transformerFactory.newTransformer();   DOMSource source = new DOMSource(document);   // 5. Create a new StreamResult to the output stream you want to use.   StreamResult result = new StreamResult(new File("/Users/admin/Desktop/users.xml"));   // StreamResult result = new StreamResult(System.out); // to print on console   // 6. Use transform method to write the DOM object to the output stream.   transformer.transform(source, result);     System.out.println("File created successfully"); }}


<?xml version="1.0" encoding="UTF-8" standalone="no"?><users>  <user uid="1">     <firstName>Interview</firstName>     <lastName>Bubble</lastName>     <email></email>  </user></users>