Implementing the createTextNode() method in DOM API.
In this section, you will see how to implements 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 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: