Navigating XML nodes in VBScript, for a Dummy

后端 未结 1 859
别那么骄傲
别那么骄傲 2020-12-01 09:52

I am trying to write a script that will manipulate some data for me in an xml file. I am pretty new to VBScript but have a VB.NET and VBA background, so I feel like I kind

相关标签:
1条回答
  • 2020-12-01 10:22

    Here is a small example:

    Suppose you have a file called C:\Temp\Test.xml with this contents:

    <?xml version="1.0"?>
    <root>
       <property name="alpha" value="1"/>
       <property name="beta" value="2"/>
       <property name="gamma" value="3"/>
    </root>
    

    Then you can use this VBScript:

    Set objDoc = CreateObject("MSXML.DOMDocument")
    objDoc.Load "C:\Temp\Test.xml"
    
    ' Iterate over all elements contained in the <root> element:
    
    Set objRoot = objDoc.documentElement
    s = ""
    t = ""
    For Each child in objRoot.childNodes
       s = s & child.getAttribute("name") & " "
       t = t & child.getAttribute("value") & " "
    Next
    MsgBox s    ' Displays "alpha beta gamma "
    MsgBox t    ' Displays "1 2 3 "
    
    ' Find a particular element using XPath:
    
    Set objNode = objDoc.selectSingleNode("/root/property[@name='beta']")
    MsgBox objNode.getAttribute("value")     ' Displays 2
    

    I hope this helps getting you started with VBScript and XML.

    0 讨论(0)
提交回复
热议问题