Home Xml Dom Creating DOM Child Elements

Related Tutorials


 
 

Share on Google+Share on Google+

Creating DOM Child Elements

Advertisement
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);

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());
  }


  }
}
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2005

Related Tutorials

Discuss: Creating DOM Child Elements   View All Comments

Post your Comment


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

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

...I'm having problems with this, import java.io.File;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 = db.build(XmlFile); Element[] e = dc.getRootElements(); System.out.println("Root " + e); } catch (Exception e) { System.out.println("Exception : " + e.getMessage()); } }} in Line Document dc = db.build(XmlFile); exception say's (Exception : Uncompilable source code) please help me with this thanks
DMCA.com