使用 selectSingleNode 选择后如何遍历节点的子节点

How do I iterate over a node's child nodes after selecting it with selectSingleNode

我正在尝试 select 来自 XML 文档的单个节点 selectSingleNode(),然后在该节点上使用 selectNodes 进一步 select 子节点该节点的项目:

option explicit

sub main() ' {

   dim doc as new MSXML2.DOMDocument

   doc.loadXML(                                                                                     _
     "<items>"                                                                                    & _
     "  <item id='1000'><name val='ABC'/><name val='DEF'/><name val='GHI'/><foo>xxx</foo></item>" & _
     "  <item id='1001'><name val='JKL'/><name val='MNO'/><name val='PQR'/><bar>yyy</bar></item>" & _
     "  <item id='1002'><name val='STU'/><name val='VWX'/><name val='YZ.'/><baz>zzz</baz></item>" & _
     "</items>")

    dim item as msxml2.IXMLDOMElement
    set item = doc.selectSingleNode("//item[@id='1002']")

    dim names as msxml2.IXMLDOMSelection
    set names = item.selectNodes("//name")

    dim name as msxml2.IXMLDOMElement
    for each name in names
        debug.print(name.getAttribute("val"))
    next name

end sub ' }

我预计这段代码会打印属性值 STUVWXYZ.。但是,当 运行 它时,它会打印每个 <name>val 值。

显然,selectNodes() select 是根文档中的所有节点。

我不明白为什么会这样,以及我如何才能获得先前 selected 节点的 真实 子节点。

您想迭代下面所示的 xpath 返回的选择。它本质上是一个包含属性节点的节点集。

Option Explicit
Public Sub main()
   Dim doc As New MSXML2.DOMDocument60

   doc.LoadXML ( _
     "<items>" & _
     "  <item id='1000'><name val='ABC'/><name val='DEF'/><name val='GHI'/><foo>xxx</foo></item>" & _
     "  <item id='1001'><name val='JKL'/><name val='MNO'/><name val='PQR'/><bar>yyy</bar></item>" & _
     "  <item id='1002'><name val='STU'/><name val='VWX'/><name val='YZ.'/><baz>zzz</baz></item>" & _
     "</items>")

    Dim items As IXMLDOMSelection, item As Object
    Set items = doc.SelectNodes("//*[@id='1002']/name/@*") 'all attribs. Or, //*[@id='1002']/name/@val for only val attributes
    For Each item In items
        Debug.Print item.text
    Next
End Sub

如果你想要更详细的方法

Option Explicit
Public Sub main()
    Dim doc As New MSXML2.DOMDocument60

    doc.LoadXML ( _
                "<items>" & _
                "  <item id='1000'><name val='ABC'/><name val='DEF'/><name val='GHI'/><foo>xxx</foo></item>" & _
                "  <item id='1001'><name val='JKL'/><name val='MNO'/><name val='PQR'/><bar>yyy</bar></item>" & _
                "  <item id='1002'><name val='STU'/><name val='VWX'/><name val='YZ.'/><baz>zzz</baz></item>" & _
                "</items>")

    Dim item As Object, attrib As Object, child As Object
    Set item = doc.SelectSingleNode("//*[@id='1002']")

    For Each child In item.ChildNodes
        For Each attrib In child.Attributes
            If attrib.name = "val" Then Debug.Print attrib.name, attrib.text
        Next
    Next
End Sub

你甚至可以乱搞:

For Each child In item.ChildNodes
    If child.BaseName = "name" And child.getAttribute("val") <> vbNullString Then Debug.Print child.getAttribute("val")
Next

改变

Set names = item.selectNodes("//name")

Set names = item.SelectNodes("name")