METHOD:  Document::createTextNode

Document.createTextNode(data)

The createTextNode method creates a new Text object whose value is the data passed as the parameter. This object, however, is not automatically included in the document tree but must be specifically inserted into it using one of the insertion methods: insertBefore, replaceChild or appendChild. Its parentNode property is therefore set to null.

To demonstrate this method, the following example uses the 'names.xml' file and creates a new 'name' element and a new text node with the value of 'Felix' which is appended to it. The new 'name' element is then appended to the document's root element, and finally the variable objCurrNode is set to the first child of the root's last child (the text of the newly-appended 'name' element), and its type, name and value are displayed.

XML:
<names>
   <name>Alice</name>
   <name>Bert</name>
   <name>Charlie</name>
   <name>Diane</name>
   <name>Eric</name>
</names>

Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("names.xml")

Dim objCurrNode, objNewNode, objNewText
Set objNewNode = objXMLDoc.createElement("name")
Set objNewText = objXMLDoc.createTextNode("Felix")
objNewNode.appendChild(objNewText)

Set objCurrNode = objXMLDoc.documentElement
objCurrNode.appendChild(objNewNode)
Set objCurrNode = objCurrNode.lastChild.firstChild
document.write(objCurrNode.nodeType)
document.write("<br>" & objCurrNode.nodeName)
document.write("<br>" & objCurrNode.nodeValue)

Output:
3
#text
Felix

The value '3' returned for the node's type indicates that it is a Text object (see the list of node types).



Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information