PC Review


Reply
Thread Tools Rate Thread

How could I parse this XML?

 
 
Anthony P.
Guest
Posts: n/a
 
      12th Jul 2010
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>(E-Mail Removed)</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>(E-Mail Removed)</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



 
Reply With Quote
 
 
 
 
Branco Medeiros
Guest
Posts: n/a
 
      12th Jul 2010
Anthony wrote:
<snip>
> I have this piece of XML:
>
> <result>success</result>
> <message_count>2</message_count>
> <messages>
> * *<message>
> * * * * <sender_name>Joe Parnell</sender_name>
> * * * * <sender_email>some...@example.com</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>some...@example.com</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 <messages> twice, but you only close it once.

> 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.
 
Reply With Quote
 
Anthony P.
Guest
Posts: n/a
 
      16th Jul 2010
Thank you Branco!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse XML Sami Microsoft C# .NET 3 5th Aug 2008 10:40 PM
Weird CultureInfo - DateTime.Parse() and Decimal.Parse() Rico Microsoft C# .NET 8 22nd Sep 2006 10:59 AM
Parse from the Right =?Utf-8?B?UEE=?= Microsoft Excel Worksheet Functions 6 11th Jun 2006 06:05 PM
Use CType or Integer.Parse to parse a string into an integer =?Utf-8?B?VGVyZXNh?= Microsoft VB .NET 8 27th Jan 2005 03:21 PM
int.parse and double.parse yield different value for hex string =?Utf-8?B?S2V2aW4=?= Microsoft C# .NET 1 15th Sep 2004 08:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:03 AM.