I am trying to parse names and phone numbers from an xml file. I can get the names just fine but I can't get the phone numbers worked out. I am able to get all the phone numbers for the first name, but the same numbers are returned for all the other names (I'm not incrementing to the phone numbers for the next name).
Here is a snippet of my xml:
Here is my VBA:
I'm sure I'm missing something elementary. Does anyone spot it?
Here is a snippet of my xml:
Code:
<contact>
<id>1</id>
<name>Mel</name>
<memory>Phone</memory>
<phone_number>
<designation>Mobile</designation>
<value>5551234567</value>
<speeddial>3</speeddial>
</phone_number>
<phone_number>
<designation>Home</designation>
<value>5552345678</value>
</phone_number>
<phone_number>
<designation>Work</designation>
<value>5551235632</value>
</phone_number>
</contact>
<contact>
<id>2</id>
<name>Tom</name>
<memory>Phone</memory>
<phone_number>
<designation>Mobile</designation>
<value>5556987896</value>
<speeddial>4</speeddial>
</phone_number>
</contact>
Here is my VBA:
Code:
Dim xmlDoc As DOMDocument
Dim phonelog As IXMLDOMNodeList
Dim contactlist As IXMLDOMNodeList
Dim individualcontact As IXMLDOMNodeList
Dim individualcontactnumber As IXMLDOMNodeList
Dim item As IXMLDOMNode
Dim phonenumber As IXMLDOMNode
Dim sName As String
Dim sNumber As String
Set xmlDoc = New DOMDocument
xmlDoc.Load vFile
Set phonelog = xmlDoc.DocumentElement.SelectNodes("report")
Set contactlist = phonelog.NextNode.SelectNodes("contacts")
Set individualcontact = contactlist.NextNode.SelectNodes("contact")
Set individualcontactnumber = individualcontact.NextNode.SelectNodes("phone_number")
For Each item In individualcontact
'Get the contact name
Select Case (item.SelectSingleNode("name").Text)
Case Is = ""
sName = "Unknown"
Debug.Print sName
Case Else
sName = item.SelectSingleNode("name").Text
Debug.Print sName
End Select
'Get the contact numbers
Select Case (item.SelectSingleNode("phone_number") Is Nothing)
Case True
'Do Nothing
Case False
For Each phonenumber In individualcontactnumber
Select Case (phonenumber.SelectSingleNode("value").Text = "")
Case True
sNumber = ""
Debug.Print sNumber
Case False
sNumber = (phonenumber.SelectSingleNode("value").Text)
Debug.Print sNumber
End Select
Next
End Select
Next
Last edited: