How can I traverse and read values in an XML using VBScript?
-
Given the xml snippet below, I am trying to read the Company value. I have other code which will obtain the Network Policy Zone and the Default Gateway of a given system. Using these two values, zone first, then dfgw, I need to get the Company value which correspondes so it can be set as an environment variable in a remote windows system. Here's my mess of code, and I'm afraid it is a mess because I've been trying different things:
Sub ReadXML()
Dim attrValue, child1, child2, gChild1
getZone()
WScript.Echo "Zone is: " & zoneSet xDoc = CreateObject("Microsoft.XMLDOM") 'CreateObject("Msxml.DOMDocument") CreateObject("Microsoft.XMLDOM") xDoc.async = False xDoc.load ("company.xml") 'Load XML document Set Root = xDoc.documentElement Set NodeList = Root.getElementsByTagName(zone) Set child1 = xDoc.documentElement.firstChild Set child2 = child1.nextSibling Set gChild1 = child1.firstChild Do Until attrValue <> dfgw attrValue = xDoc.GetAttrValue("DFGW") WScript.Echo("DFGW Attribute Value is: " & attrValue) xDoc.nextSibling Loop company = gChild1.nodeValue("Company") WScript.Echo("Company Attribute Value is: " & attrValue) 'Set ElemList = xDoc.getElementsByTagName("Zone0") 'dfgw = ElemList.item(0).getAttribute("DFGW") WScript.Echo("Default Gateway: " & dfgw) 'company = "BMC Software, Inc." SetCompany(company)
End Sub
Here's the XML I'm trying to read:
<?xml version="1.0" ?> - <ZoneList> - <Zone0> - <DFGW> "192.168.25.1" <Company>"CPC"</Company> </DFGW> - <DFGW> "142.101.230.1" <Company>"Test Company"</Company> </DFGW> - <DFGW> "" <Company>""</Company> </DFGW> - <DFGW> "" <Company>""</Company> </DFGW> - <DFGW> "" <Company>""</Company> </DFGW> </Zone0> - <Zone1>
-
Given the xml snippet below, I am trying to read the Company value. I have other code which will obtain the Network Policy Zone and the Default Gateway of a given system. Using these two values, zone first, then dfgw, I need to get the Company value which correspondes so it can be set as an environment variable in a remote windows system. Here's my mess of code, and I'm afraid it is a mess because I've been trying different things:
Sub ReadXML()
Dim attrValue, child1, child2, gChild1
getZone()
WScript.Echo "Zone is: " & zoneSet xDoc = CreateObject("Microsoft.XMLDOM") 'CreateObject("Msxml.DOMDocument") CreateObject("Microsoft.XMLDOM") xDoc.async = False xDoc.load ("company.xml") 'Load XML document Set Root = xDoc.documentElement Set NodeList = Root.getElementsByTagName(zone) Set child1 = xDoc.documentElement.firstChild Set child2 = child1.nextSibling Set gChild1 = child1.firstChild Do Until attrValue <> dfgw attrValue = xDoc.GetAttrValue("DFGW") WScript.Echo("DFGW Attribute Value is: " & attrValue) xDoc.nextSibling Loop company = gChild1.nodeValue("Company") WScript.Echo("Company Attribute Value is: " & attrValue) 'Set ElemList = xDoc.getElementsByTagName("Zone0") 'dfgw = ElemList.item(0).getAttribute("DFGW") WScript.Echo("Default Gateway: " & dfgw) 'company = "BMC Software, Inc." SetCompany(company)
End Sub
Here's the XML I'm trying to read:
<?xml version="1.0" ?> - <ZoneList> - <Zone0> - <DFGW> "192.168.25.1" <Company>"CPC"</Company> </DFGW> - <DFGW> "142.101.230.1" <Company>"Test Company"</Company> </DFGW> - <DFGW> "" <Company>""</Company> </DFGW> - <DFGW> "" <Company>""</Company> </DFGW> - <DFGW> "" <Company>""</Company> </DFGW> </Zone0> - <Zone1>
XPath? Here's some VBscript that uses XPath to select the nodes you want
Dim attrValue, child1, child2, gChild1
zone = "Zone0"
dfgw = """192.168.25.1"""
WScript.Echo "Zone is: " & zone
WScript.Echo "dfgw is: " & dfgwSet xDoc = CreateObject("Microsoft.XMLDOM") 'CreateObject("Msxml.DOMDocument") CreateObject("Microsoft.XMLDOM")
xDoc.async = False
xDoc.load ("a.xml") 'Load XML documentxpathString = "//" & zone & "/DFGW[text() = '" & dfgw & "']/Company"
WScript.Echo xpathStringSet CompanyNodeList = xDoc.selectNodes(xpathString)
WScript.Echo CompanyNodeList.LengthYou just need to be sure that the string values are correctly escaped when building the query. Also - why the quotes around the string values?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p