The Text object represents the text of an Element or
an Attr object. (In XML this is referred to as character data as
opposed to any markup that might be used to modify it.) If the text does contain markup, it
is parsed into a list of markup elements with accompanying child text nodes.
When a document is first made available to the DOM, there is only one Text node
for each block of text. Users may subsequently add adjacent Text nodes to represent
the content of an Element without any intervening markup, but
should be aware that there is no way to represent the separations between these nodes in
XML or HTML, and that they will not persist between DOM editing sessions.
If you wish to maintain a DOM structure between sessions, it is recommended that you
use the Element object's
normalize method to combine adjacent
Text nodes into one single one.
In the following example we load the 'albums.xml' file and append a new Text child
node to the 'title' element of the last album. The code then iterates through that element's
children displaying the value of each.
XML:
<Albums>
<Album ref="CD142" category="Folk">
<title>Boil The Breakfast Early</title>
<artist>The Chieftains</artist>
</Album>
<Album ref="CD720" category="Pop">
<title>Come On Over</title>
<artist>Shania Twain</artist>
</Album>
<Album ref="CD024" category="Country">
<title>Red Dirt Girl</title>
<artist>Emmylou Harris</artist>
</Album>
</Albums>
Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("albums.xml")
Set Elem = objXMLDoc.documentElement.childNodes.item(2).firstChild
Set Text = objXMLDoc.createTextNode("Move along, nothing to see here.")
Elem.appendChild(Text)
numChildren = Elem.childNodes.length
For i = 0 To numChildren-1
document.write(Elem.childNodes.item(i).nodeValue & "<br>")
Next
Output:
Red Dirt Girl
Move along, nothing to see here.
A Text object is also a Node object, and so inherits
various properties and methods from it. For details of the values returned by the
nodeName,
nodeType and nodeValue
properties for a Text object, see the Node object.
denotes a Microsoft extension to the W3C DOM.
PROPERTIES
attributes Property
This is a read-only property that returns an
NamedNodeMap for nodes that can have
attributes.
Syntax: Node.attributes
baseName Property
This is a read-only property that returns the base name for a node.
Syntax: Node.baseName
childNodes Property
This is a read-only property containing a node list of all children for those elements that
can have them.
Syntax: Node.childNodes
data Property
This property contains the data for this node, depending on node type.
Syntax: CharacterData.data
dataType Property
This is a read-only property that specifies the data type for the node.
Syntax: Node.dataType
definition Property
This property returns the definition of the node in the DTD or schema.
Syntax: Node.definition
firstChild Property
This is a read-only property that returns the first child node of a node. If there is none,
it returns null.
Syntax: Node.firstChild
lastChild Property
This is a read-only property that returns the last child node of a node. If there is none,
it returns null.
Syntax: Node.lastChild