This method sets the value of the named attribute.
The setAttribute method sets the value of the named
attribute. If an attribute of the same name already exists in the
element, then its value is changed to that of the 'value' parameter; if
not, one is created. The value is a simple string and is not parsed
when it is set. Therefore, any markup (such as syntax to be recognized
as an entity reference) is treated as literal text and needs to be
escaped by the implementation when it is written out.
To assign an attribute that contains entity references, the user
must build an appropriate subtree of an Attr object and any Text and EntityReference objects, and assign it using the setAttributeNode
method.
XML:
<Vocabulary>
<Word
type="noun"
level="1">
<English>house</English>
<French>maison</French>
<Spanish>casa</Spanish>
</Word>
<Word
type="verb"
level="1">
<English>go</English>
<French>aller</French>
<Spanish>ir</Spanish>
</Word>
</Vocabulary>
VBScript:
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("vocabulary.xml")
Set Elem = objXMLDoc.documentElement.firstChild
Elem.setAttribute "level", "2"
Set NamedNodeMap = Elem.attributes
For Each Attr In NamedNodeMap
document.write(Attr.value
"<br>")
Next
noun
2
In this example, we load the 'vocabulary.xml' file and call the setAttribute method to alter the 'level' attribute of the first 'Word' element to '2'. The code then displays the values of all the attributes.