Creating DOM Child Elements

This lesson shows you how to create root and child elements in the DOM tree.

Creating DOM Child Elements


This lesson shows you how to create root and child elements in the DOM tree. We will first create a blank DOM document and add the root element to it. Then I show you how to add comment element and then child element to the root element. In this example following xml code will generated and displayed on the console.

<?xml version="1.0" encoding="UTF-8" ?> 
This is comment--> 
Child attribute1="The value of Attribute 1" /> 

Creating the root element

In the previous lesson you learned how to create DocumentBuilder object and the create a blank DOM document. The following code creates a blank document.

//Create blank DOM Document
Document doc = docBuilder.newDocument();

The createElement function is used to create the root element and appendChild method is used to append the element to the DOM document.

//create the root element
Element root = doc.createElement("root");
//all it to the xml tree

Adding Comment Element to DOM Tree

The doc.createComment function is used to create Comment object.

//create a comment
Comment comment = doc.createComment("This is comment");
//add in the root element

Here is the video insturction "How to add child element in XML (dom) using Java?":

Adding Child Element to DOM Tree

The doc.createElement function is used to create Child element.

//create child element
Element childElement = doc.createElement("Child");
//Add the atribute to the child
childElement.setAttribute("attribute1","The value of Attribute 1");

Printing the DOM Tree on console

An finally we will print the DOM tree on the console with the following code:

TransformerFactory tranFactory = TransformerFactory.newInstance(); 
Transformer aTransformer = tranFactory.newTransformer(); 

Source src = new DOMSource(doc); 
Result dest = new StreamResult(System.out); 
aTransformer.transform(src, dest); 

Here is the full code of

import org.w3c.dom.*;

import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 

class CreateDomXml 
  public static void main(String[] args
  //Create instance of DocumentBuilderFactory
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  //Get the DocumentBuilder
  DocumentBuilder docBuilder = factory.newDocumentBuilder();
  //Create blank DOM Document
  Document doc = docBuilder.newDocument();

  //create the root element
  Element root = doc.createElement("root");
  //all it to the xml tree
  //create a comment
  Comment comment = doc.createComment("This is comment");
  //add in the root element

  //create child element
  Element childElement = doc.createElement("Child");
  //Add the atribute to the child
  childElement.setAttribute("attribute1","The value of Attribute 1");

  TransformerFactory tranFactory = TransformerFactory.newInstance()
  Transformer aTransformer = tranFactory.newTransformer()

  Source src = new DOMSource(doc)
  Result dest = new StreamResult(System.out)
  aTransformer.transform(src, dest)

  }catch(Exception e){


Download source code of the project in Eclipse Project format.

Share on Google+Share on Google+

Creating DOM Child Elements

Posted on: April 14, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Creating DOM Child Elements   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Bhanu Prakash
May 26, 2011

But, can we dynamically generate an XML file at the desired path? Using Java.
May 7, 2012
Exception need help

...I'm having problems with this, import;import javax.swing.text.Document;import javax.swing.text.Element;import org.jdom2.input.DOMBuilder;public class ReadXMLFile { public static void main(String[] ar) { try { File XmlFile = new File("C:/Documents and Settings/Carlo/Desktop/Converter Air Hong Kong/Stud.xml"); DOMBuilder db = new DOMBuilder(); Document dc =; Element[] e = dc.getRootElements(); System.out.println("Root " + e); } catch (Exception e) { System.out.println("Exception : " + e.getMessage()); } }} in Line Document dc =; exception say's (Exception : Uncompilable source code) please help me with this thanks