DOM Parser: How to create an XML file in java

In this tutorial, we show you how to use DOM Parser to create an XML.

DOM Parser  has several classes to create an XML file. First, you need to create a Document with the class DocumentBuilder, set XML content node, attribute with the class Element. Finally, you must use the Transformer class to pass the XML content into the output stream.

At the end, the following file named exemple.xml must be created:

 
< Directory>
< contact id="1">
< Name> encoder
< First name> java
< mobile> 098745126
< Email> codeurjava8@gmail.com


The class that creates an XML:

import java.io.File; 
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 javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXML{

public static void main(String argv[]) {

try {

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();

// root element
Document doc = docBuilder.newDocument();
Root Element = doc.createElement("directory");
doc.appendChild(root);

// the contact
Contact element = doc.createElement("contact");
root.appendChild(contact);

// attributes of the contact
Attr attr = doc.createAttribute("id");
attr.setValue("1");
contact.setAttributeNode(attr);

// the name
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("coder"));
contact.appendChild(name);

// first name
First name element = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("java"));
contact.appendChild(first name);

// mobile
Mobile Element = doc.createElement("mobile");
mobile.appendChild(doc.createTextNode("098745126"));
contact.appendChild(mobile);

// the email
Element email = doc.createElement("email");
email.appendChild(doc.createTextNode("codeurjava8@gmail.com"));
contact.appendChild(email);

// 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("monFichier.xml"));

transformer.transform(source, result);

System.out.println("File Backed Up Successfully!");

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
Output:

File saved successfully!
A new file monFichier.xml was created in the current directory, with a default encoding UTF-8.

How to create an XML file in Java

You can create the attribute with its value directly:

attr.setValue("1"); 
contact.setAttributeNode(attr);
equivalent to:

contact.setAttribute("id", "1");