createElement Method


Creates an element node using the specified name.

JScript Syntax

var objXMLDOMElement = oXMLDOMDocument.createElement(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.


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

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");  
var root;  
var newElem;  
xmlDoc.async = false;  
if (xmlDoc.parseError.errorCode != 0) {  
   var myErr = xmlDoc.parseError;  
   WScript.Echo("You have error " + myErr.reason);  
} else {  
   root = xmlDoc.documentElement;  
   newElem = xmlDoc.createElement("NEW");  
   root.childNodes.item(0).lastChild.text = "123";  



C/C++ Syntax

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


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

The address of the IXMLDOMElement interface for the new element.

Return Values

The value returned if successful.

The value returned if the element parameter is Null.

The value returned if an error occurs.


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.


Implemented in: MSXML 3.0 and MSXML 6.0

Applies to


See Also

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