Parsing XML (1 Viewer)

haydenal

Registered User.
Local time
Today, 03:03
Joined
Jan 8, 2009
Messages
52
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:
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
I'm sure I'm missing something elementary. Does anyone spot it?
 
Last edited:

jardiamj

Registered User.
Local time
Today, 01:03
Joined
Apr 15, 2009
Messages
59
As far as I can see you are missing a loop through all the contacts in contactlist like:

Code:
For Each individualcontact In contactlist
      For Each item In individualcontact
      ...............
      Next
Next
I think that's what you are missing.
 

haydenal

Registered User.
Local time
Today, 03:03
Joined
Jan 8, 2009
Messages
52
I am using:

Code:
For Each item In individualcontact
    For Each phonenumber In individualcontactnumber
    .............
    Next
Next


It is returning each contact (as I expect) and each number for the first contact but it returns the phonenumbers for the first contact for every contact, it's not going to the next contact when it's looking for the phonenumber.
 

wazz

Super Moderator
Local time
Today, 16:03
Joined
Jun 29, 2004
Messages
1,711
i'd go along with the previous responder.
 

haydenal

Registered User.
Local time
Today, 03:03
Joined
Jan 8, 2009
Messages
52
Maybe I'm misunderstanding. For Each individualcontact In contactlist gives me a type mismatch.
 

wazz

Super Moderator
Local time
Today, 16:03
Joined
Jun 29, 2004
Messages
1,711
i see. it's not the actual syntax i agree with it's the missing loop. i'm not sure of the vba syntax for dealing with xml but there does seem to be a loop missing. sry i can't help more than that.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:03
Joined
Sep 12, 2006
Messages
15,658
i was messing about with xml and didnt do it this way - i didnt realise you could get at nodes like this - what i have is a recursive alogrithm doing this sort of thing, which you may find useful. this works with any tree type data structure - and you will certainly visit every node in the xml file (tree), as an xml has a hierarchical "tree" structure

Code:
traversenode(basenode as IXMLDOMNode)
processnode ' test for what sort of node you have in this bit
if basenode.haschildren then
   for each node in basenode.children
      traversenode(node)
   next
end if
end sub

sub main
dim root as node
   set root to be base of entire tree
   traverse(root)
end sub
 

jardiamj

Registered User.
Local time
Today, 01:03
Joined
Apr 15, 2009
Messages
59
Hello again!
I like the way the Siberian Husky friend does it, cause it works if you don't know how the structure of your XML tree is. But in this case the code you posted should work well, I'm almost sure you are just missing a loop. It seems to me you are looping through all the items for each contact but not through every contact in the contact list.
Guive it a try and tell me if it works.
 

Users who are viewing this thread

Top Bottom