VBS 按属性名称搜索 XML 节点,更改子属性 [英] VBS search XML node by attribute name, change child attribute

查看:40
本文介绍了VBS 按属性名称搜索 XML 节点,更改子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 XML 文件的精简版:simple.xml

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>

我需要一个 vbscript 通过它的属性名称=Write_1080p"找到正确的模块"节点,然后将其子节点的属性val"更改为禁用".

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".

应该很简单,但我是 VB 脚本的新手并且即将癫痫发作.

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

推荐答案

这个脚本:

  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

输出:

<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>

展示了如何使用 .setProperty "SelectionLanguage", "XPath" 来确保处理 XPath 查询,如何查询属性值 (..t/module[@name=""Write_1080P""]/opt..),以及如何读取 (.getAttribute("val")) 和写入 (.setAttribute "val",禁用")一个属性.

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).

这篇关于VBS 按属性名称搜索 XML 节点,更改子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆