Share on Google+Share on Google+

Implementing the createTextNode() method in DOM API.

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

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; 

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



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



Download The Example:


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


Advertisement null