XML DOM » Objects » Text

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.

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.

Examples

Code:
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>

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.
Explanation:

In this 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.

Language(s): VBScript XML

Properties

data

Syntax: characterData.data

This property contains the data for this node, depending on node type.

length

Syntax: characterData.length

This property is read-only and contains the length of the data string in characters.

Methods

appendData

Syntax: characterData.appendData(data)

This method appends the specified string to existing string data.

deleteData

Syntax: characterData.deleteData(offset, count)

This method is used to remove the specified range of characters from string data.

insertData

Syntax: characterData.insertData(offset, data)

This method is used to insert a string at the specified offset.

replaceData

Syntax: characterData.replaceData(offset, count, data)

This method replaces the characters from the specified offset with the supplied string data.

splitText

Syntax: text.splitText(offset)

This method splits a Text node into two at the specified offset. It places all the characters from the offset to the end of the string into a new Text node that immediately follows this one.

substringData

Syntax: characterData.substringData(offset, count)

This method returns a substring consisting of the specified range of characters.

See Also: