Поделиться через


createAttribute Method

 

Creates a new attribute with the specified name.

JScript 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.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);  
   }  
}  

Output

id=""  

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.

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.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

Applies to

IXMLDOMDocument-DOMDocument

See Also

createNode Method
namespaceURI Property (IXMLDOMNode)
setAttribute Method
ownerDocument Property
parentNode Property1
nodeType Property1
nodeName Property1
IXMLDOMElement
IXMLDOMAttribute