Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

vbscript - VBS search XML node by attribute name, change child attribute

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

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

I need a vbscript find the correct "module" node by it's attribute name="Write_1080p" and then change the attribute "val" of his child node "disabled".

Should be quite simple, but I'm new to scripting in VB and am about to have a seizure.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This script:

  Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..estdataxmlso11781815.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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...