VBScript to modify an Element (of many, with same Element names but with different Attributes)?

前端 未结 1 599
傲寒
傲寒 2021-01-26 17:15

I need to write a simple VBScript to modify an existing XML file. I am able to write a VBScript to modify an element, but the problem I am currently encountering is that I have

相关标签:
1条回答
  • 2021-01-26 17:43

    Use an XPath search expression that referes to the name attribute, selectSingleNode(), and getAttributeNode() as in:

      Dim oFS      : Set oFS      = CreateObject("Scripting.FileSystemObject")
      Dim sFSpec   : sFSpec       = goFS.GetAbsolutePathName("..\testdata\xml\so14541579.xml")
      Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument.6.0")
      objMSXML.setProperty "SelectionLanguage", "XPath"
      objMSXML.async = False
    
      objMSXML.load sFSpec
    
      If 0 = objMSXML.parseError Then
         Dim sXPath : sXPath    = "/MyDoc/Section[@name=""Second""]/Parameter"
         Dim ndFnd  : Set ndFnd = objMSXML.selectSingleNode(sXPath)
         If ndFnd Is Nothing Then
            WScript.Echo sXPath, "not found"
         Else
            WScript.Echo ndFnd.getAttributeNode("value").value
            ndFnd.getAttributeNode("value").value = "abracadabra.dll"
            WScript.Echo objMSXML.xml
         End If
      Else
         WScript.Echo objMSXML.parseError.reason
      End If
    

    output:

    MsrNdp.dll
    <MyDoc>
            <Section name="First">
                    <Parameter name="Service" value="MsrNdp.dll"/>
            </Section>
            <Section name="Second">
                    <Parameter name="Service" value="abracadabra.dll"/>
            </Section>
    </MyDoc>
    
    0 讨论(0)
提交回复
热议问题