VBA循环遍历XML节点

时间:2015-04-20 23:08:19

标签: xml excel vba excel-vba

你好我试图在xml节点之间循环但由于某种原因我只得到第一个节点任何想法将不胜感激!     XML:

<root>
          <AC>
           <answer id ="c1"> 
              blue
           </answer>
           <answer id ="c1"> 
              blue
           </answer>
</root>

VBA代码:

     Set ques = getQues(qName) 'ques is in CreateObject("MSXML2.DOMDocument") format

            Set nodes = ques.SelectNodes("/root/AC")
            For Each node In nodes
                MsgBox (node.SelectSingleNode("answer").Text)
                Cells(i + 1, j).Value = node.SelectSingleNode("answer").Text
            Next node

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您的selectnodes仅选择AC节点:看起来您真的想要选择answer个节点的集合 -

Sub Tester()

Dim d As New MSXML2.DOMDocument
Dim nodes, nd

    d.LoadXML Range("A1").Value

    Set nodes = d.SelectNodes("/root/AC/answer")
    For Each nd In nodes
        Debug.Print nd.getAttribute("id"), nd.Text
    Next nd

End Sub