Document.createAttribute(name)
The createAttribute 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.
To demonstrate this, the following 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.
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>
Code (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"
|