Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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


  }
}

                         

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (post your own) View All Comments Latest 10 Comments:

To create a XML Document and tree structure for my project entitled as Tamilnadu Tourism.

Posted by jeya on Thursday, 08.5.10 @ 13:22pm | #98714

Good.. thankyou

Posted by Renjan on Wednesday, 02.4.09 @ 00:53am | #84468

this is good but as per my requirement I create the XML database but how could I implement the query in XML database using core java?

Posted by Rudra Prasad Lahiri on Monday, 12.29.08 @ 07:15am | #83262

thanks a lot for helping me in xml-dom.

Posted by venkat on Friday, 01.4.08 @ 15:46pm | #44504

Plz Help Me
I want to edit xml file by Java
If any one have any idia about this then help;

Posted by Mukesh on Thursday, 12.20.07 @ 15:51pm | #43103

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]