Freigeben über


createElement Method

 

Creates an element node using the specified name.

JScript 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 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;  
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;  
   newElem = xmlDoc.createElement("NEW");  
   root.childNodes.item(0).appendChild(newElem);  
   root.childNodes.item(0).lastChild.text = "123";  
   WScript.Echo(root.childNodes.item(0).xml);  
}  

Output

<child><NEW>123</NEW></child>  

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.

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.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

Applies to

IXMLDOMDocument-DOMDocument

See Also

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