循环遍历 XML 文件元素 [英] Looping through XML file elements
本文介绍了循环遍历 XML 文件元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面有一个 XML 文件.我想遍历此文件并提取节点节点值,例如 for node <com>
获取名称值,然后循环 2 次以获取文件值.我目前可以获取节点 <com>
的值,但不确定如何在内部循环并获取文件节点的值.
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
推荐答案
您可以使用 .ChildNodes
属性
Dim xmlDoc, objNodeList, plot
dim fileNodes
dim comNodeItem
dim fileNodeItem
dim fileName, jobName
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("C:\test\combineXML.xml")
WScript.Echo xmlDoc.parseError
Set objNodeList = xmlDoc.getElementsByTagName("com")
For each comNodeItem in objNodeList
JobName = comNodeItem.getAttribute("name")
for each fileNodeItem in comNodeItem.ChildNodes
fileName = fileNodeItem.getAttribute("name")
WScript.Echo JobName & ": " & fileName
next
Next
如果文件像您的示例一样简单,这将起作用.如果您只想处理 file
节点,而忽略其他节点,您也可以再次使用:
This 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:
for each fileNodeItem in comNodeItem.getElementsByTagName("file")
这篇关于循环遍历 XML 文件元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文