XML DOM » Document » createAttribute

Syntax:
document.createAttribute(name)
name
Receives the name that will be given to the newly created Attr object.

This method creates an Attr object of the specified name.

It does not, however, add a value for that attribute. This must be done separately using the Element object's setAttribute method. An attribute so created will have a parentNode property set to null until it has been 'set' into an element.

Examples

Code:
XML:

<library>
   <book>
      <category>fiction</category>
      <title>Eyeless in Gaza</title>
      <author>Aldous Huxley</author>
   </book>
   <book>
      <category>classics</category>
      <title>John Barleycorn</title>
      <author>Jack London</author>
   </book>
</library>

VBScript:

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("library.xml")

Dim objNamedNodeMap, objCurrNode, objIdAttr, objCatAttr, index
Set objIdAttr = objXMLDoc.createAttribute("ID")
Set objCatAttr = objXMLDoc.createAttribute("catalog")
Set objCurrNode = objXMLDoc.documentElement.firstChild
objCurrNode.setAttribute "ID", "0173"
objCurrNode.setAttribute "catalog", "DL-3678"

Set objNamedNodeMap = objCurrNode.attributes
For index = 0 To objNamedNodeMap.length-1
   document.write(objNamedNodeMap.item(index).xml & "<br>")
Next
Output:
ID="0173"
catalog="DL-3678"
Explanation:

This example creates two attributes ('ID' and 'catalog') and sets them in the first 'book' element. A NamedNodeMap is created from this element and then the code loops through each attribute and displays the associated XML.

Language(s): VBScript XML

See Also: