这是我的代码片段,这部分是读取XML文件并获取Operations的最后一个子属性。在这种情况下,我想得到类型C.事情是脚本跳过了整个For
循环,正如我所说的回声所证明的那样。我做了一些搜索,但仍然无法找出我的代码有什么问题。
Set FSO = CreateObject("Scripting.FileSystemObject")
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
counter = 0
xmlDoc.Load(mostrecent(i).Name)
Set colNodes = xmlDoc.SelectNodes("/Runs/Run/Operations")
WScript.Echo counter '<--appears
For Each objNode In colNodes
WScript.Echo counter '<--didn't appear
If Attr.Exists(objNode.LastChild.GetAttribute("type")) Then
counter = counter + 1
WScript.Echo counter
End If
Next
XML:
<Runs>
<Run>
<Operations>
<Operation type="A"></Operation>
<Operation type="B"></Operation>
<Operation type="C"></Operation>
</Operations>
</Run>
</Runs>
答案 0 :(得分:1)
尝试使用这段代码获取父节点type
的最后一个子节点的Operations
属性
Dim objXML, strPath, objCol
strPath = mostrecent(i).Name
Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async=False
objXML.load strPath
strQuery = "/Runs/Run/Operations/Operation"
Set objCol = objXML.selectNodes(strQuery) 'collection of all the <Operation> nodes
MsgBox objCol.item(objCol.length-1).attributes.getnameditem("type").text
更新: 对我来说很好:
答案 1 :(得分:0)
如果您想要使用Type = C获取所有操作,可以使用以下代码
Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
With objXML
.SetProperty "SelectionLanguage", "XPath"
.ValidateOnParse = True
.Async = False
.Load "C:\Users\Pankrit\Desktop\test.xml"
End With
Set nodesC = objXML.DocumentElement.SelectNodes("/Runs/Run/Operations/Operation[@type='C']")
If nodesC.Length >= 1 Then
For Each nodeC In nodesC
MsgBox nodeC.NodeName
Next
End If