I have been all over the web looking for a clean way to change 2 attributes in an XML node using VBS (unfortunately my only choice for this with this client). The XML has a node in it that looks like this:
<lot id="ajhgkhga" lot="1" block="1" section="73R">
I need to change it to this (and retain the rest of the XML document):
<lot id="ajhgkhga" lot="1" block="22" section="55">
I have been hacking away at VBS most of the day and here is where I am now:
Dim objXMLDoc
Dim objXMLElement
Dim objXMLNodeList
Dim numObjXMLNodeList
Dim i
Dim lot
Dim section
Dim attr
set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = "false"
objXMLDoc.load("section73R.xml")
Set objXMLNodeList = objXMLDoc.getElementsByTagName("lot")
numObjXMLNodeList = objXMLNodeList.length
For i = 0 to numObjXMLNodeList - 1
' current value of the block and section attributes of the lot element
block = objXMLNodeList.item(i).getAttribute("block")
section = objXMLNodeList.item(i).getAttribute("section")
'Wscript.Echo block & " " & section
' new value of the block and section attributes of the lot element
objXMLNodeList.item(i).setAttribute "block", "22"
objXMLNodeList.item(i).setAttribute "section", "55"
Next
This throws an error as indicated above. My VBS chops are barely existent at this point, I have not done it in a very long time. The code above is designed only to try to read the attributes, I figured that once I had that replacing the values wouldn’t be too hard.
Can anyone help me actually figure out how to replace those values? And in doing so show me how far off I am in this script? Thanks in advance!
EDIT: I can now get the two attribute values out (see code change above). All I need now is a way to write the new attribute values to the XML file and this will be a done deal. Can anyone give me any pointers?
EDIT #2: I was able to solve it pretty quickly once I was able to retrieve the attribute values and I have edited the code above.
It’s unclear what the structure of your document is. I was able to change attributes using this code. Obviously, I don’t have multiple “lots” in my document so you may need to iterate through your nodes to find the one that you want to edit. This should get you going though: