Creating DOM Child Elements

Ads
 

Creating DOM Child Elements

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

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" ?> 
<root>
<!--
This is comment--> 
<
Child attribute1="The value of Attribute 1" /> 
</
root>

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
doc.appendChild(root);

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
root.appendChild(comment);

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");
root.appendChild(childElement);

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 CreateDomXml.java

import org.w3c.dom.*;

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


class CreateDomXml 
{
  public static void main(String[] args
  {
  try{
  //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
  doc.appendChild(root);
  
  //create a comment
  Comment comment = doc.createComment("This is comment");
  //add in the root element
  root.appendChild(comment);

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

  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){
  System.out.println(e.getMessage());
  }


  }
}

Download source code of the project in Eclipse Project format.

Ads

Ads