VBS search XML node by attribute name, change child attribute

前端 未结 1 688
感动是毒
感动是毒 2020-12-07 05:51

this is a stripped down version of my XML file: simple.xml


 
  
   
    
     <         


        
相关标签:
1条回答
  • 2020-12-07 06:12

    This script:

      Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
      Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\testdata\xml\so11781815.xml")
      Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument")
      oXML.setProperty "SelectionLanguage", "XPath"
      oXML.async = False
      oXML.load sFSpec
      If 0 = oXML.parseError Then
         WScript.Echo oXML.xml
         WScript.Echo "-----------------"
         Dim sXPath : sXPath    = "/project/scenes/scene/rootgroup/nodelist/module[@name=""Write_1080P""]/option/disabled"
         Dim ndFnd  : Set ndFnd = oXML.selectSingleNode(sXPath)
         If ndFnd Is Nothing Then
            WScript.Echo sXPath, "not found"
         Else
            WScript.Echo ndFnd.nodeName, ndFnd.getAttribute("val")
            WScript.Echo "-----------------"
            ndFnd.setAttribute "val", "disabled"
            WScript.Echo oXML.xml
         End If
      Else
         WScript.Echo oXML.parseError.reason
      End If
    

    output:

    <project>
            <scenes>
                    <scene>
                            <rootgroup>
                                    <nodelist>
                                            <module type="WRITE" name="Write_1080P">
                                                    <option>
                                                            <disabled val="true"/>
                                                    </option>
                                            </module>
                                    </nodelist>
                            </rootgroup>
                    </scene>
            </scenes>
    </project>
    
    -----------------
    disabled true
    -----------------
    <project>
            <scenes>
                    <scene>
                            <rootgroup>
                                    <nodelist>
                                            <module type="WRITE" name="Write_1080P">
                                                    <option>
                                                            <disabled val="disabled"/>
                                                    </option>
                                            </module>
                                    </nodelist>
                            </rootgroup>
                    </scene>
            </scenes>
    </project>
    

    shows how to use .setProperty "SelectionLanguage", "XPath" to make sure that XPath queries are processed, how to query for an attribute value (..t/module[@name=""Write_1080P""]/opt..), and how to read (.getAttribute("val")) and write (.setAttribute "val", "disabled") an attribute.

    P.S. Look here to see how you can look for/change text (with essentially the same code).

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