METHOD:  NamedNodeMap::setNamedItem

NamedNodeMap.setNamedItem(newItem)

The setNamedItem method adds the specified item to the collection using its nodeName property. If that item is not an attribute then an error is returned. This method returns the attribute successfully added to the collection, at the same time replacing an existing one, if it has the same name. The new attribute node will subsequently be accessible through its nodeName property.

The following example uses the 'staff.xml' file. It removes the 'pay' attribute of the first 'employee' element and then uses the setNamedItem method to replace the 'pay' attibute of the last 'employee' element with it. Finally the attributes of both elements are displayed. Note that although the 'pay' attribute of the first element is removed, the code still displays a value for it. This is because it has a default value of '1' in the DTD, which immediately replaces it (see the removeNamedItem method).

XML:
<staff>
   <employee ssn="123456" pay="3">
      <f_name>John</f_name>
      <l_name>Sullivan</l_name>
   </employee>
   <employee ssn="987654" pay="2">
      <f_name>Mary</f_name>
      <l_name>Lopez</l_name>
   </employee>
</staff>

Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("staff.xml")

Set FirstEmployee = objXMLDoc.documentElement.firstChild
Set LastEmployee = objXMLDoc.documentElement.lastChild
Set FirstAttributes = FirstEmployee.attributes
Set LastAttributes = LastEmployee.attributes

Set PayAttr = FirstAttributes.removeNamedItem("pay")
LastAttributes.setNamedItem(PayAttr)
For Each Attr In FirstAttributes
   document.write(Attr.nodeValue & ", ")
Next
document.write("<br>")
For Each Attr In LastAttributes
   document.write(Attr.nodeValue & ", ")
Next

Output:
123456, 1
987654, 3



Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information