简体   繁体   English

找到具有特定属性的XML节点并进行更改

[英]Locate XML node with specific attribute and change it

I have looked through several threads here on how to locate an XML node and change an attribute. 我在这里浏览了几条有关如何找到XML节点和更改属性的线程。 However, I cannot find a solution to my problem: 但是,我找不到解决问题的方法:

I have an XML node which has several nodes under it with the same name (but different attributes). 我有一个XML节点,该节点下有几个具有相同名称(但属性不同)的节点。 For example: 例如:

<Configuration>
  <ConfigOptions>
    <add key="Localize" value="Off" />
    <add key="Cache" value="Database" />
    <add key= etc........

I need to use a simple VBScript to locate the <add> node where key="Cache" and then change the value to something else. 我需要使用一个简单的VBScript在<add>节点上找到key="Cache" ,然后将该值更改为其他值。

use xpath syntax to select the node 使用xpath语法选择节点

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
xmlDoc.load strXMLPath
Set node = xmlDoc.selectNodes("//Configuration/ConfigOptions/add[@key='Cache']")
strOldValue = node.item(0).attributes.getNamedItem("value").text

then change the value 然后更改值

node.item(0).attributes.getNamedItem("value").text = strNewValue

then save the xml file 然后保存xml文件

xmlDoc.save strXMLPath

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

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