Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. How can I traverse and read values in an XML using VBScript?

How can I traverse and read values in an XML using VBScript?

Scheduled Pinned Locked Moved XML / XSL
questionsysadminxmlannouncementworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curtis Rowell
    wrote on last edited by
    #1

    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: " & zone

    Set 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>

    S 1 Reply Last reply
    0
    • C Curtis Rowell

      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: " & zone

      Set 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>

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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: " & dfgw

      Set xDoc = CreateObject("Microsoft.XMLDOM") 'CreateObject("Msxml.DOMDocument") CreateObject("Microsoft.XMLDOM")
      xDoc.async = False
      xDoc.load ("a.xml") 'Load XML document

      xpathString = "//" & zone & "/DFGW[text() = '" & dfgw & "']/Company"
      WScript.Echo xpathString

      Set CompanyNodeList = xDoc.selectNodes(xpathString)
      WScript.Echo CompanyNodeList.Length

      You 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups