Easy learning with example program codes

dom parser to create xml file in java

The DOM refers to Document Object Model. The DOM parses the XML file, creates the DOM objects and loads it into the memory in the tree structure.

Note: DOM is simple and easy to use but it consume lots of memory and slow.

The XML refers to the EXtensible Markup Language which is used to store and transport the data. XML not provides any predefined tags. We have to define our own tags. Every opening XML tag must have a closing tag. XML tags are case sensitive and must be completely nested.

Steps to create a new XML file:

1. Create a Document with DocumentBuilder class.
2. Define the XML data using Element class.
3. Write the whole data on a file using Transformer class.


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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;
 * This class is used to create XML document using DOM parser.
 * @author codesjava
public class DOMParserCreateTest {
	public static void main(String args[]) {
		try {
		//File Path
		String filePath = "D:\\class.xml";
		//Create DocumentBuilderFactory object.
		DocumentBuilderFactory dbFactory = 
		//Get DocumentBuilder object.
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		//Get Document object.
		Document document = dBuilder.newDocument();
		//Define root element.
		Element rootElement = document.createElement("class");
		//Define students element.
		Element students = document.createElement("students");
		//Set attribute to element.
		Attr attr = document.createAttribute("className");
		//Define StudentName elements.
		Element student1 = document.createElement("studentName");
		Element student2 = document.createElement("studentName");
		student2.appendChild(document.createTextNode("Nidhi Gupta"));
		//Write content into XML file.
		TransformerFactory transformerFactory = 
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(document);
		StreamResult result = new StreamResult(new File(filePath));
		transformer.transform(source, result);
		//For console Output.
		StreamResult consoleResult = new StreamResult(System.out);
		transformer.transform(source, consoleResult);			
	    } catch (Exception e) {


<?xml version="1.0" encoding="UTF-8"?>
<students className="MCA">
<studentName>Nidhi Gupta</studentName>

Download this example.
Next Topic: DOM XML parser to modify xml file in java.
Previous Topic: DOM XML parser to parse XML file in java.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki