2015-04-20 155 views
0

你好我正在嘗試從xml節點之間循環,但出於某種原因,我只得到第一個節點任何想法將不勝感激! XML:通過XML節點的VBA循環

<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 

謝謝您的幫助!

+2

你的XML不能很好地形成 - 缺少收盤'' –

回答

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 
+0

真棒這個是否感謝你的幫助!我編輯了我的代碼,效果很棒! – paul590