createElement Method

MSXML 5.0 SDK

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

createElement Method

Creates an element node using the specified name.

[Script]

Script Syntax

var objXMLDOMElement = oXMLDOMDocument.createElement(tagName);

Parameters

tagName
A string specifying the name for the new element node. The name is case-sensitive. This name is subsequently available as the element node's nodeName property.

Return Value

An object. Returns the IXMLDOMElement object for the new element.

Example

The following script example creates an element called PAGES and appends it to an IXMLDOMNode object. It then sets the text value of the element to 400.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var root;
var newElem;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   root = xmlDoc.documentElement;
   newElem = xmlDoc.createElement("PAGES");
   root.childNodes.item(0).appendChild(newElem);
   root.childNodes.item(0).lastChild.text = "400";
   alert(root.childNodes.item(0).xml);
}
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMElement = oXMLDOMDocument.createElement(tagName)

Parameters

tagName
A string specifying the name for the new element node. The name is case-sensitive. This name is subsequently available as the element node's nodeName property.

Return Value

An object. Returns the IXMLDOMElement object for the new element.

Example

The following Microsoft® Visual Basic® example creates an element called PAGES and appends it to an IXMLDOMNode object. It then sets the text value of the element to 400.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim root As IXMLDOMElement
Dim newElem As IXMLDOMElement
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 root = xmlDoc.documentElement
   Set newElem = xmlDoc.createElement("PAGES")
   root.childNodes.Item(0).appendChild newElem
   root.childNodes.Item(0).lastChild.Text = "400"
   MsgBox root.childNodes.Item(0).xml
End If
[C/C++]

C/C++ Syntax

HRESULT createElement(
    BSTR tagName,
    IXMLDOMElement **element);

Parameters

tagName [in]
The name for the new element node. It is case-sensitive. This name is subsequently available as the element node's nodeName property.
element [out,retval]
The address of the IXMLDOMElement interface for the new element.

C/C++ Return Values

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

Remarks

Creating an element with this method is the same as using createNode where the type parameter value is NODE_ELEMENT and no namespace is specified.

You cannot create a namespace-qualified element using the createElement method. Regardless of whether a namespace prefix is included in the tagName parameter, the namespaceURI property for the new element node is set to an empty string, "". An element node constructed as part of an XML document load operation will never have both a prefix and an empty namespace Uniform Resource Identifier (URI). You can only create a namespace-qualified element using the createNode method of the DOMDocument object.

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 nodeType property has the value NODE_ELEMENT.

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

createNode Method | namespaceURI Property (IXMLDOMNode) | ownerDocument Property | parentNode Property | nodeName Property | nodeType Property | insertBefore Method | replaceChild Method | appendChild Method | IXMLDOMElement

Applies to: DOMDocument