Share on Google+Share on Google+

Creates a New DOM Parse Tree

In this section, describes a method to create a new DOM tree.

Creates a New DOM Parse Tree


This Example describes a method to create a new DOM tree .Methods which are used for making a new DOM parse tree are described below :-

Element root = doc.createElement("places"):-it is a method to Create an Element node.

doc.appendChild(root):-This method adds a node after the last child node of the specified element root.

Element root = doc.getDocumentElement():-allows direct access to the root of the DOM document.

Xml code for the program generated is:-

<?xml version="1.0" encoding="UTF-8"?>
    Document   : Document6.xml
    Created on : 10 July, 2008, 5:20 PM
    Author     : girish
        Purpose of the document follows.

 * @Program that Creates a New DOM Parse Tree
 * Author:-RoseIndia Team
 * Date:-10-Jun-2008

import javax.xml.parsers.*;
import org.w3c.dom.*;

public class Parsetree {

  public static void main(String[] args) throws Exception {
  DocumentBuilderFactory builderFactory = 

  Document doc = builderFactory.newDocumentBuilder().parse(
  new Parsetree().buildTree(doc);

  public void buildTree(Document doc) {
  Element Companyname;
  Text text;
  Element root = doc.createElement("Company");
  Companyname = doc.createElement("Level");
  text = doc.createTextNode("SoftwareDevelopment");
  Companyname = doc.createElement("Location");
  text = doc.createTextNode("Rohini");
  Element root1=doc.getDocumentElement();
  System.out.print("Name of the root created is:- "+


Output of the program:-

Name of the root created is:- Company

DownLoad Source Code


Posted on: August 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Creates a New DOM Parse Tree  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
November 1, 2011

My hat is off to your astute cmmoand over this topic-bravo!