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

createNode Method

Creates a node using the supplied type, name, and namespace.

[Script]

Script Syntax

var objXMLDOMNode = oXMLDOMDocument.createNode(Type, name, namespaceURI);

Parameters

Type
A variant. A value that uniquely identifies the node type. This can be specified using either the integer value or the string value. For the complete list of values, see XML DOM Enumerated Constants or the description for the name parameter.
name
A string containing the value for the new node's nodeName property. The relationship between the name and Type parameters is summarized in the Remarks section of this topic.
namespaceURI
A string defining the namespace URI. If specified, the node is created in the context of the namespaceURI parameter with the prefix specified on the node name. If the name parameter does not have a prefix, this is treated as the default namespace.

Return Value

Object. Returns the newly created node.

Example

The following script example creates an attribute node called Sci-Fi and adds it to the attributes for the DOMDocument object.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var MyNode;
var namedNodeMap;
xmlDoc.async = false;
xmlDoc.load("books.xml");
MyNode = xmlDoc.createNode(2, "Sci-Fi", "");
namedNodeMap = xmlDoc.documentElement.childNodes.item(0).attributes;
namedNodeMap.setNamedItem(MyNode);
alert(xmlDoc.documentElement.xml);
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMNode = oXMLDOMDocument.createNode(Type, name, namespaceURI)

Parameters

Type
A variant. A value that uniquely identifies the node type. This can be specified using either the integer value or the string value. For the complete list of values, see the XML DOM Enumerated Constants or the description for the name parameter.
name
A string containing the value for the new node's nodeName property. The relationship between the name and Type parameters is summarized in the Remarks section of this topic.
namespaceURI
A string defining the namespace URI. If specified, the node is created in the context of the namespaceURI parameter with the prefix specified on the node name. If the name parameter does not have a prefix, this is treated as the default namespace.

Return Value

An object. Returns the newly created node.

Example

The following Microsoft® Visual Basic® example creates an attribute node called Sci-Fi and adds it to the attributes for the DOMDocument object.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim MyNode As IXMLDOMNode
Dim namedNodeMap As IXMLDOMNamedNodeMap
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 MyNode = xmlDoc.createNode(NODE_ATTRIBUTE, "Sci-Fi", "")
   Set namedNodeMap = xmlDoc.documentElement.childNodes.Item(0).Attributes
   namedNodeMap.setNamedItem MyNode
   MsgBox (xmlDoc.documentElement.xml)
End If
[C/C++]

C/C++ Syntax

HRESULT createNode(
    VARIANT Type,
    BSTR name,
    BSTR namespaceURI,
    IXMLDOMNode **node);

Parameters

Type [in]
The value that uniquely identifies the node type. This can be specified using either the integer value or the string value. For the complete list of values, see the XML DOM Enumerated Constants or the description given for the name parameter.
name [in]
The value for the new node's nodeName property. The relationship between the name and Type parameters is summarized in the Remarks section of this topic.
namespaceURI [in]
The namespace URI. If specified, the node is created in the context of the namespaceURI parameter with the prefix specified on the node name. If the name parameter does not have a prefix, this is treated as the default namespace.
node [out, retval]
The newly created node.

C/C++ Return Values

S_OK
The value returned if successful.
E_INVALIDARG
The value returned if the node parameter is Null.
E_FAIL
The value returned if an error occurs.

Remarks

The name parameter depends on the value of the Type parameter:

NODE_ATTRIBUTE The name of the attribute.
NODE_CDATA_SECTION, NODE_COMMENT, NODE_DOCUMENT, NODE_DOCUMENT_FRAGMENT, NODE_TEXT The nodeName property for these node types is a constant value; the name parameter is ignored.
NODE_DOCUMENT_TYPE The name of the document type; for example, the xxx in <!DOCTYPE xxx ...>.
NODE_ELEMENT The name of the XML tag, with any namespace prefix included if present.
NODE_ENTITY The name of the entity.
NODE_ENTITY_REFERENCE The name of the entity referenced. The name does not include the leading ampersand or the trailing semicolon. The name includes the namespace, if one is present.
NODE_NOTATION The name of the notation.
NODE_PROCESSING_INSTRUCTION The target, the first token following the <? characters.

You cannot create a node of type NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_ENTITY, or NODE_NOTATION.

When a node is created, it is created in the context of a namespace if one is supplied (if the namespaceURI parameter is supplied). If one is not supplied, the node is created in the namespace of the document. If the namespaceURI parameter is specified, the node is created in the context of the namespaceURI parameter with the prefix specified on the node name.

For node types that do not have names, the empty string, "", should be passed as the name parameter.

For elements and entity references, when the value of the namespaceURI parameter is anything other than "", and the value of the name parameter does not contain a prefix (xxx in xxx:yyy), the value of the namespaceURI parameter is treated as the default namespace.

Attributes cannot be scoped to a default namespace, and other elements are not qualified to a particular namespace; they are treated as being from the namespace defined by the document itself.

When the value of namespaceURI parameter is the empty string, "", the node is created without a namespace. Creating a qualified node without specifying a nonempty namespaceURI returns an error. This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).

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

nodeName Property

Applies to: DOMDocument