xml 在 vbscript 中选择具有属性名称的单个节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4370640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Single Node with a attribute name in vbscript
提问by fireBand
Have an xml file
有一个xml文件
<DataSource>
<localdata>
<add context="Localization">
<parameter name="timeout" type="int" defaultvalue="60"/>
<parameter name="address" type="string" defaultvalue="192.168.9.45" />
<parameter name="port" type="int" defaultvalue="6789"/>
</add>
<add context="General">
<parameter name="timeout" type="int" defaultvalue="60"/>
<parameter name="address" type="string" defaultvalue="192.168.9.478" />
<parameter name="port" type="int" defaultvalue="5674"/>
</add>
</localdata>
</DataSource>
I need to get the element whose attribute is context="General"using vbscript
我需要获取其属性context="General"使用 vbscript的元素
I can get the top node with this statement
我可以用这个语句得到顶级节点
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load("DataConfiguration.xml")
Set queryNode = xmlDocument.selectSingleNode(".//localdata")
But not sure how to extend this.
但不确定如何扩展它。
Any help is appreciated.
任何帮助表示赞赏。
Thanks in advance.
提前致谢。
回答by
To get any node, you can use this
要获取任何节点,您可以使用它
Set queryNode = xmlDocument.selectSingleNode(".//node()[@context = 'General']")
or, specifically for the addnode
或者,专门针对add节点
Set queryNode = xmlDocument.selectSingleNode(".//add[@context = 'General']")
This is using XPath, which may require you to set the selection namespace property of the DomDocument
这是使用 XPath,这可能需要您设置 DomDocument 的选择命名空间属性
xmlDocument.setProperty "SelectionLanguage", "XPath"
You might want to search for a XPath tutorial, such as w3schools- New Link
您可能想搜索 XPath 教程,例如w3schools- 新链接

