0

Hello I am trying to loop between nodes from an xml but for some reason I am only getting the first node any ideas would be greatly appreciated! XML:

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

VBA Code:

     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

Thank you for your help!

1
  • 2
    Your XML is not well-formed - missing the closing </AC> Commented Apr 20, 2015 at 23:16

1 Answer 1

3

Your selectnodes is only selecting the AC node: it looks like you really want to select the collection of answer nodes -

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.