createAttribute Method

MSXML 5.0 SDK

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

createAttribute Method

Creates a new attribute with the specified name.

[Script]

Script Syntax

var objXMLDOMAttribute = oXMLDOMDocument.createAttribute(name);

Parameters

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

Return Value

An object. Returns the new IXMLDOMAttribute object.

Example

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.5.0");
var root;
var newAtt;
var namedNodeMap;
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;
   newAtt = xmlDoc.createAttribute("ID");
   namedNodeMap = root.attributes;
   namedNodeMap.setNamedItem(newAtt);
   for (var i=0; i<namedNodeMap.length; i++) {
      alert(namedNodeMap.item(i).xml);
   }
}
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMAttribute = oXMLDOMDocument.createAttribute(name)

Parameters

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

Return Value

An object. Returns the new IXMLDOMAttribute object.

Example

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

Dim xmlDoc As New Msxml2.DOMDocument50
Dim root As IXMLDOMElement
Dim newAtt As IXMLDOMAttribute
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 root = xmlDoc.documentElement
   Set newAtt = xmlDoc.createAttribute("ID")
   Set namedNodeMap = root.Attributes
   namedNodeMap.setNamedItem newAtt
   For i = 0 To (namedNodeMap.length - 1)
      MsgBox (namedNodeMap.Item(i).xml)
   Next
End If
[C/C++]

C/C++ Syntax

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

Parameters

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.

C/C++ Return Values

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.

Remarks

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.

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) | setAttribute Method | ownerDocument Property | parentNode Property | nodeType Property | nodeName Property | IXMLDOMElement | IXMLDOMAttribute

Applies to: DOMDocument