简体   繁体   English

VBS通过属性名称搜索XML节点,更改子属性

[英]VBS search XML node by attribute name, change child attribute

this is a stripped down version of my XML file: simple.xml 这是我的XML文件的精简版本: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". 我需要一个vbscript通过其属性名称=“ Write_1080p”找到正确的“模块”节点,然后更改其子节点“已禁用”的属性“ val”。

Should be quite simple, but I'm new to scripting in VB and am about to have a seizure. 应该很简单,但是我是VB脚本开发的新手,而且很快就会被抓住。

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. 显示如何使用.setProperty "SelectionLanguage", "XPath"确保已处理XPath查询,以及如何查询属性值( ..t/module[@name=""Write_1080P""]/opt.. )以及如何读取( .getAttribute("val") )和写入( .setAttribute "val", "disabled" )属性。

PS Look here to see how you can look for/change text (with essentially the same code). PS查看此处 ,了解如何查找/更改文本(使用基本相同的代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM