I have a XML file below. I want to loop through this file and extract the node node value, like for node <com>
get the name value and then loop 2 times to get the file values. I can currently get the value for the node <com>
but am unsure how to loop inside and get the values for file node.
<common>
<com name="Test1.css">
<file name="Tech.css"/>
<file name="Comp.css"/>
</com>
<com name="Test2.css">
<file name="HR.css"/>
<file name="HR2.css"/>
</com>
</common>
Dim xmlDoc, objNodeList, plot
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("C:\test\combineXML.xml")
WScript.Echo xmlDoc.parseError
Set objNodeList = xmlDoc.getElementsByTagName("com")
If objNodeList.length > 0 then
For each x in objNodeList
JobName = x.getattribute("name")
WScript.Echo JobName
Next
End If
You're making this too complicated. Just select the
name
attribute from the child nodes of allcom
nodes with an XPath expression:Use
//com/file/@name
if you need the expression to be more specific (in case there are other child nodes with aname
attribute.If you also want attributes from a parent node, you'll have to modify it like this:
You can use the
.ChildNodes
PropertyThis will work if the file is as simple as your example. If you want only
file
nodes to be processed, and others ignored, you can also just use again: