METHOD:  NamedNodeMap::setNamedItem


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).

   <employee ssn="123456" pay="3">
   <employee ssn="987654" pay="2">

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

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

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

123456, 1
987654, 3

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