Home Tutorial Xml Dom Implementing the createTextNode() method in DOM API.

 
 

Share on Google+Share on Google+
Implementing the createTextNode() method in DOM API.
Posted on: September 23, 2010 at 12:00 AM
Advertisement
In this section, you will see how to implements createTextNode method in DOM API.

Implementing the createTextNode() method in DOM API

In this illustration, we will discuss about the appending the value to the node in XML document.

The createTextNode() method creates a new Text for the specified node as the document element for Text node.

DocumentBuilderFactory class is used to create new DOM parsers and  DocumentBuilder is used in code given below for creating an Blank DOM Document.

The following code is used to create a textNode.

Text childElement = doc.createTextNode("string");

Here is the full code of createTextNodeExample.java

import org.w3c.dom.*;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

class createTextNodeExample 
{
  public static void main(String[] args
  {
    try{
   
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    Element root = doc.createElement("roseindia");
       doc.appendChild(root);
    Text elmnt=doc.createTextNode("Employee");
       root.appendChild(elmnt);
    Text childElement = doc.createTextNode(":spring");
          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());
         }
     }
}

Output:

C:\>javac createTextNodeExample.java

C:\>java createTextNodeExample
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<roseindia>

       Employee:spring 

</roseindia>

Download The Example:

Advertisement

Related Tags for Implementing the createTextNode() method in DOM API.:


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: September 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com