Поделиться через


createTextNode Method

 

Creates a text node that contains the supplied data.

JScript Syntax

var objXMLDOMText = oXMLDOMDocument.createTextNode(data);  

Parameters

data
A string specifying the value to be supplied to the new text object's nodeValue property.

Return Value

An object. Returns the new IXMLDOMText object.

Example

The following script example creates an IXMLDOMText object, and then uses the insertBefore method to place it before the first child node of the document.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var root;
var MyText;
var MyNewNode;
xmlDoc.async = false;
xmlDoc.loadXML("<root><child/></root>");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   root = xmlDoc.documentElement;
   MyText = xmlDoc.createTextNode("Hello World");
   MyNewNode = root.insertBefore(MyText, root.childNodes.item(0));
   WScript.Echo(xmlDoc.xml);
}

Output

<root>Hello World!<child/></root>

Visual Basic Syntax

Set objXMLDOMText = oXMLDOMDocument.createTextNode
(data)  

Parameters

data
A string specifying the value to be supplied to the new text object's nodeValue property.

Return Value

An object. Returns the new IXMLDOMText object.

Example

The following Microsoft® Visual Basic® example creates an IXMLDOMText object, and then uses the insertBefore method to place it before the first child node of the document.

Dim xmlDoc As New Msxml2.DOMDocument30
Dim root As IXMLDOMElement
Dim MyText As IXMLDOMText
Dim MyNewNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.loadXML "<root><child/></root>"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set root = xmlDoc.documentElement
   Set MyText = xmlDoc.createTextNode("Hello World")
   Set MyNewNode = root.insertBefore(MyText, root.childNodes.Item(0))
   MsgBox (xmlDoc.xml)
End If

Output

<root>Hello World!<child/></root>

C/C++ Syntax

HRESULT createTextNode(  
    BSTR data,  
    IXMLDOMText **text);  

Parameters

data[in]
The value to be supplied to the new text's nodeValue.

text[out, retval]
The address of new IXMLDOMText.

Return Values

S_OK
The value returned if successful.

E_INVALIDARG
The value returned if the text parameter is Null.

E_FAIL
The value returned if an error occurs.

Remarks

Creating a text node with this method is the same as using createNode where the Type parameter value is NODE_TEXT and no namespace is specified. You cannot specify a namespace with the createTextNode method.

Although this method creates the new object in the context of this document, it does not automatically add the new object to the document tree. (In other words, although the ownerDocument property of the new node points to this document object, the parentNode property is set to Null.) To add the new object, you must explicitly call one of the node insert methods, insertBefore method, replaceChild method, or appendChild method.

The new nodeType property has the value NODE_TEXT.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

Applies to

IXMLDOMDocument-DOMDocument

See Also

nodeValue Property
IXMLDOMText
createNode Method
ownerDocument Property
parentNode Property1
nodeType Property1
insertBefore Method
replaceChild Method
appendChild Method