Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


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

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
XML
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Creates element node, attribute node, comment node, processing instruction and a CDATA section

                         

This Example shows you how to Create an Element node ,Comment node ,Attribute node, Processing node and CDATA section node in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers. These are some of the methods used in code given below for adding attribute:-

Element root = doc.createElement("Company"):-This method creates an element node ,Here Company specifies the name for the element node.

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

Comment node = doc.createComment("Comment for company"):-This method creates an Comment node ,Here "Comment for company" specifies the name for the Comment node.

CDATASection cdata = doc.createCDATASection("Roseindia <, >, .net rohini"):- This method creates a CDATASection node. Here string "Roseindia <, >, .net rohini" specifies the data for the node

Xml code for the program generated is:-

<?xml version="1.0" encoding="UTF-8"?>
<root></root>

CreatesElementnode.java:-

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

public class CreatesElementnode {

    public static void main(String[] argsthrows Exception {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        Document doc = builderFactory.newDocumentBuilder().parse(new File("abc.xml"));
        new CreatesElementnode().CreatesElementnode(doc);
    }

    public void CreatesElementnode(Document doc) {
        //method to creates an element node.
        Element root = doc.createElement("Company");
        doc.appendChild(root);
        System.out.println("Element node created is: " 
                                     doc.getDocumentElement
().getNodeName());

        //method to creates a comment node.
        Comment node = doc.createComment("Comment for company");
        root.appendChild(node);
        System.out.println("Comment node created is: " + root.getFirstChild());

        //Create a Cdata section node
        CDATASection cdata = doc.createCDATASection("Roseindia <, >, .net rohini");
        root.appendChild(cdata);
        System.out.println("CData node created is: " 
                                             root.getFirstChild
().getNextSibling());

        ProcessingInstruction pi = 
                            doc.createProcessingInstruction
("Roseindia""Rohini");
        root.appendChild(pi);
        System.out.println("ProcessingInstruction node created is: " 
                                                              root.getLastChild
());
    }
}


Output of the program:-

Element node created is: Company
Comment node created is: [#comment: Comment for company]
CData node created is: [#cdata-section: Roseindia <, >, .net rohini]
ProcessingInstruction node created is: [Roseindia: Rohini]

   
DownLoad SourceCode

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
meter
actionlistener
how to align columns i
primitive data types u
tiles in jsp applicati
udp client java
xhtml
Java Count Vowels in t
how to create a dialog
java collection framew
inert date into mysql
a simple calculator pr
jdbc connection source
sample code on richtex
ASP.NET .NET Add text
delete statement form
override request
web service hibernat
List in J2ME
tables pdf itext
java combo box
code fro finding index
Hibernate EJB3 tutoria
Photoshop Photo Effect
Add RenderingHints to
Common Gataway Interfa
CalendarComponent
java popup window even
Facelet tutorial
Java Communications AP
<s:a href
jsp trim char
websphere portals
Tree2
check if java enabled
CSS tutorial
Eclipse 3.4 EJB
jsf managed bean
prepared statement poo
create file using serv
conditional
COMO USAR JOptionPane
struts 1 date picker e
<SELECT> and Post
jboss connection pool
DateFormat
JTwain
lifecycle of threads
space char
java keyboard input
clip source code
XMLmerging
ActionScript Tutorial
sockets through servle
creating user login
Flash Math Physics Mas
retreive numerical val
struts2 enum select
how to create war file
Flex Custom mxml tags
400 question
decimal to binary conv
excel from java
database connections
create bar graph in ja
400 book
date in java
java file sream
multiple ajax combo
Pizza ordering program
jsp login by insertin
dispaly date in day mo
create HTML table
weblogic server 9.2
clientside validations
java installtion
count row
create file menu using
HOW TO INSERT VALUE FO
add title in pdf
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
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

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.