createAttribute Method
TOC
Collapse the table of content
Expand the table of content

createAttribute Method

 

Creates a new attribute with the specified name.

var objXMLDOMAttribute = oXMLDOMDocument.createAttribute(name);

 name 

A string specifying the name of the new attribute object. This name is subsequently available as the new node's nodeName property.

An object. Returns the new IXMLDOMAttribute object.

The following script example creates a new attribute called ID and adds it to the attributes of the DOMDocument object.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
var root;
var newAtt;
var namedNodeMap;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   root = xmlDoc.documentElement;
   newAtt = xmlDoc.createAttribute("ID");
   namedNodeMap = root.attributes;
   namedNodeMap.setNamedItem(newAtt);
   for (var i=0; i<namedNodeMap.length; i++) {
      WScript.Echo(namedNodeMap.item(i).xml);
   }
}

id=""

HRESULT createAttribute(
    BSTR name,
    IXMLDOMAttribute **attribute);

 name[in]

The name of the new attribute object. This name is subsequently available as the new node's nodeName property.

 attribute[out, retval]

The address of the new IXMLDOMAttribute object.

S_OK

The value returned if successful.

E_INVALIDARG

The value returned if the attribute parameter is Null.

E_FAIL

The value returned if an error occurs.

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

You cannot create a namespace-qualified attribute using the createAttribute method. Regardless of whether a namespace prefix is included in the name parameter, the namespaceURI property for the new attribute is set to an empty string, "". An attribute 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 attribute using the createNode method of the DOMDocument.

No data value is set for the attribute during the create operation. You can set the value by calling the setAttribute method of the element 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 remains null. To associate the attribute with an element, call the setAttributeNode method of the IXMLDOMElement object.

Because the parentNode property of an attribute always returns a Null value, this property will not change after associating the new attribute with an element using the setAttribute method.

The nodeType property has the value NODE_ATTRIBUTE.

Implemented in: MSXML 3.0 and MSXML 6.0

         IXMLDOMDocument/DOMDocument       

Show:
© 2016 Microsoft