How could I parse this XML?

A

Anthony P.

Hello Everyone,

I'm having a bit of trouble and I'm hoping someone here will be able
to give me some direction. I've spent a good two hours on Google and
simply can't figure this out.

I have this piece of XML:

<result>success</result>
<message_count>2</message_count>
<messages>
<message>
<sender_name>Joe Parnell</sender_name>
<sender_email>[email protected]</sender_email>
<subject><![CDATA[Beta Test - Anthony]]></subject>
<video_link>http://example.com/videos/33.3gp</video_link>
</message>

<messages>
<message>
<sender_name>Joe Parnell</sender_name>
<sender_email>[email protected]</sender_email>
<subject><![CDATA[Beta Test #2 - Anthony]]></subject>
<video_link>http://example.com/videos/34.3gp</video_link>
</message>

</messages>
</response>

So obvious this is the return of multiple 'message' nodes and that's
what's throwing me. How do I process these? I need to be able to parse
out each individual message. I am totally lost. Can anyone help?

Thanks!
Anthony
 
B

Branco Medeiros

Anthony wrote:
I have this piece of XML:

<result>success</result>
<message_count>2</message_count>
<messages>
   <message>
        <sender_name>Joe Parnell</sender_name>
        <sender_email>[email protected]</sender_email>
        <subject><![CDATA[Beta Test - Anthony]]></subject>
        <video_link>http://example.com/videos/33.3gp</video_link>
</message>

<messages>
   <message>
        <sender_name>Joe Parnell</sender_name>
        <sender_email>[email protected]</sender_email>
        <subject><![CDATA[Beta Test #2 - Anthony]]></subject>
        <video_link>http://example.com/videos/34.3gp</video_link>
</message>

</messages>
</response>

There are at least two errors in the xml above that will prevent it
from being parsed:

a) there's a missing <response> node that seems to be closed at the
end of the xml (<response>....</response>)
b) you open the node said:
So obvious this is the return of multiple 'message' nodes and that's
what's throwing me. How do I process these? I need to be able to parse
out each individual message. I am totally lost. Can anyone help?
<snip>

My guess is that if you correct the errors above you can feed the xml
to a XmlDocument and then read each node back:

<example
Dim Doc As New Xml.XmlDocument
Doc.LoadXml(Source) '<-- your xml text here

Dim Response As Xml.XmlNode = Doc.DocumentElement

Debug.Print("Result: {0}", _
GetNodeText(Response, "result"))
Debug.Print("Message count: {0}", _
GetNodeText(Response, "message_count"))

For Each Node As Xml.XmlNode _
In Response.SelectNodes("messages/message")
Debug.Print("Message:")
Debug.Print(" sender_name: {0}", _
GetNodeText(Node, "sender_name"))
'...
'I guess you know how to go from here
'...
Debug.Print("")
Next

'--------------------------------------------------------
Function GetNodeText( _
ByVal Parent As Xml.XmlNode, _
ByVal Path As String _
) As String
'--------------------------------------------------------
Dim N As Xml.XmlNode = Parent.SelectSingleNode(Path)
If N IsNot Nothing AndAlso N.FirstChild IsNot Nothing Then
Return N.FirstChild.Value
End If
Return ""
End Function
</example>

HTH.

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top