text Property

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

text Property

Represents the text content of the node or the concatenated text representing the node and its descendants.

[Script]

Script Syntax

strValue = oXMLDOMNode.text;

Example

The following script example creates an IXMLDOMNode of type NODE_ENTITY, and then displays the object's text value, including that of any of the object's child nodes.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var currNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   currNode = xmlDoc.documentElement.childNodes.item(0);
   alert(currNode.text);
}
[Visual Basic]

Visual Basic Syntax

strValue = oXMLDOMNode.text

Example

The following Microsoft® Visual Basic® example creates an IXMLDOMNode of type NODE_ENTITY, and then displays the object's text value, including that of any of the object's child nodes.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim currNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.Load ("books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set currNode = xmlDoc.documentElement.childNodes.Item(0)
   MsgBox currNode.Text
End If
[C/C++]

C/C++ Syntax

HRESULT get_text(
    BSTR *text);
HRESULT put_text(
    BSTR text);

Parameters

text [out, retval]
A string representing the text content of this node and its descendants. This value varies depending on the value of the nodeType method.

C/C++ Return Values

S_OK
The value returned if successful.
S_FALSE
The value returned when there is no text.
E_INVALIDARG
The value returned if the text parameter is Null.

Remarks

String. The property is read/write. When concatenated, the text represents the contents of text or CDATA nodes. All concatenated text nodes are normalized according to xml:space attributes and the value of the preserveWhiteSpace switch. Concatenated CDATA text is not normalized. (Child nodes that contain NODE_COMMENT and NODE_PROCESSING_INSTRUCTION nodes are not concatenated.) .text trims the whitespace on the edges of the result, and "normalizes" \r\n => \n, but otherwise just concatenates text.

Retrieves and sets the string representing the text contents of this node or the concatenated text representing this node and its descendants.

For more precise control over text manipulation in an XML document, use the lower-level nodeValue property, which returns the raw text associated with a NODE_TEXT node.

Consider the <root> element in this example:

<root att="   123   a   <   ">
   <a>   a   a   </a>
   <!--   comment   b   -->
   <?pi   pi   c   ?>
   <![CDATA[   cdata   d   ]]>
   e   f
</root>

The text property for this element returns the following concatenated text.

"a a   cdata   d    e f"

Note that the white space within the CDATA node is preserved.

This value depends on the value of the nodeType property.

NODE_ATTRIBUTE

NODE_DOCUMENT

NODE_ENTITY

Returns a string representing the value of the node. This is the concatenated text of all subnodes with entities expanded.
NODE_CDATA_SECTION

NODE_COMMENT

NODE_PROCESSING_INSTRUCTION

NODE_TEXT

Returns the text contained in the node, which is the same as the nodeValue property.
NODE_DOCUMENT_TYPE

NODE_NOTATION

Returns the empty string (""). These node types do not have associated text.
NODE_DOCUMENT_FRAGMENT Returns the text comprised of the concatenation of all descendant nodes.
NODE_ELEMENT Contains a string that represents the element content. This will also include the text content from all child elements, concatenated in document order. For example, consider the following XML.
<count>
  <item>one</item>
  <item>two</item>
  <item>three</item>
  <item>four</item>
</count>

The text property for the <count> element contains the value "one two three four".

NODE_ENTITY_REFERENCE Returns the string representation of the entity reference.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

nodeType Property | nodeValue Property

Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText