我需要解析一个 xmlobject 并从节点中提取两个值,我可以从这些树中获取两个值。我被困在如何向下遍历循环中的节点以获取一个值,然后确定我是否可以获取第二个值。
这是在 VBS 中,并被用作已经运行的程序中的扩展。一直在查看此处和 Microsoft 文档,但无法从它们那里获得有关 VBS 的良好信息。
示例 XML
<Detail>
<StandardLine>
<Stan>
<Type>A</Type>
<Code>1234</Code>
<Value>sdg</Value>
</Stan>
</StandardLine>
<StandardLine>
<Stan>
<Type>C</Type>
<Code>122234</Code>
<Value>sdsdgg</Value>
<Cate>Thiere</Cate>
</Stan>
</StandardLine>
<StandardLine>
<Stan>
<Type>1</Type>
<Code>7336</Code>
<Value>this one</Value>
<Stone>diamond</Stone>
</Stan>
</StandardLine>
</Detail>
Dim xmlDoc, nodes, strQuery, objNode
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
'This is inputing the xml as a string from a previous bit of code
xmlDoc.loadXML(XMLOut)
strQuery = "//Detail/StandardLine"
Set nodes = xmlDoc.documentElement.selectNodes(strQuery)
'Check to see if this xmlDoc has any of the desired SL nodes
If nodes.length = 0 Then
msgbox "There are no SLine nodes",,"The checker"
exit function
End If
'Will only get this far if there is a SLine node
msgbox "Before Nodes" & vbCrLf & nodes.length,,"Pre loop"
' Will go through each SLinenodes
For Each objNode in nodes
msgbox "I am in the loop!",,"The box...in the loop"
'Here down is what I want to do but am not able to get it to work
'Need to to process the SLine node
Type = objNode.documentElement.selectSingleNode("//Stan/Type").text
If Type = 1 Then
Code = objNode.documentElement.selectSingleNode("//Stan/Code").text
End if
'Will be functions here later to use the variable Code
msgbox "Number is: " & Code,,"Thereas the number?"
Next
msgbox "After Nodes",,"Post loop"
如果元素的值为 1,我想从上面的 XML 中提取元素中的值。
编辑:修复了示例 xml 中放错位置的“/”