Select Single Node with attribute name in vbscript

Have an xml file

<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 an element with context="General" attribute using 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 expand it.

Any help is appreciated.

Thanks in advance.

+4
source share
1 answer

To get any node, you can use this

 Set queryNode = xmlDocument.selectSingleNode(".//node()[@context = 'General']") 

or in particular for add node

 Set queryNode = xmlDocument.selectSingleNode(".//add[@context = 'General']") 

This uses XPath, which may require setting the namespace property to select in DomDocument

 xmlDocument.setProperty "SelectionLanguage", "XPath" 

You might want to find an XPath tutorial like w3schools - New link

+6
source

All Articles