Need help parsing a twitter xml document into variables in VB.NET?
Ok so I have spent a day or so trying to get this working. I need to parse an xml document from twitter selecting certain nodes and placing the value in variables. I get an error...Conversion from string "user/screen_name" to type 'Integer' is not valid. Parsing XML is out of my league. any help would be appreciated... here is what i have so far.
First the Twitter XML file...
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<created_at>Sat Apr 30 04:10:46 +0000 2011</created_at>
<id>64179865481510912</id>
<text>@sixrevisions implementing the Twitter API for my new portfolio site!</text>
<source><a href="http://twitter.com/#!/download/iphone" rel="nofollow">Twitter for iPhone</a></source>
<truncated>false</truncated>
<favorited>false</favorited>
<in_reply_to_status_id>64087873804189696</in_reply_to_status_id>
<in_reply_to_user_id>14444403</in_reply_to_user_id>
<in_reply_to_screen_name>sixrevisions</in_reply_to_screen_name>
<retweet_count>0</retweet_count>
<retweeted>false</retweeted>
<user>
<id>92868468</id>
<name>Timothy Antonucci</name>
<screen_name>TimAtWerked</screen_name>
<location>Boston, MA</location>
<description>My name is Tim a web designer/developer in Boston, I love cars and photography ;-)</description>
<profile_image_url>http://a2.twimg.com/profile_images/1336500668/wrkdtwitico_normal.gif</profile_image_url>
<url>http://www.werked.com</url>
<protected>false</protected>
<followers_count>14</followers_count>
<profile_background_color>b2c789</profile_background_color>
<profile_text_color>333333</profile_text_color>
<profile_link_color>0084B4</profile_link_color>
<profile_sidebar_fill_color>b3d6af</profile_sidebar_fill_color>
<profile_sidebar_border_color>6e996a</profile_sidebar_border_color>
<friends_count>34</friends_count>
<created_at>Fri Nov 27 00:35:31 +0000 2009</created_at>
<favourites_count>0</favourites_count>
<utc_offset>-18000</utc_offset>
<time_zone>Eastern Time (US & Canada)</time_zone>
<profile_background_image_url>http://a3.twimg.com/profile_background_images/97017170/1440x900sm.jpg</profile_background_image_url>
<profile_background_tile>true</profile_background_tile>
<profile_use_background_image>true</profile_use_background_image>
<notifications />
<geo_enabled>false</geo_enabled>
<verified>false</verified>
<following />
<statuses_count>39</statuses_count>
<lang>en</lang>
<contributors_enabled>false</contributors_enabled>
<follow_request_sent />
<listed_count>0</listed_count>
<show_all_inline_media>false</show_all_inline_media>
<default_profile>false</default_profile>
<default_profile_image>false</default_profile_image>
<is_translator>false</is_translator>
</user>
<geo />
<coordinates />
<place />
<contributors />
</status>
</statuses>
Next my code...
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_xmld = New XmlDocument()
m_xmld.Load("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=timatwerked&count=4&include_rts=true")
m_nodelist = m_xmld.SelectNodes("/statuses/status")
For Each m_node In m_nodelist
Dim twitName = m_node.ChildNodes.Item("screen_name").InnerText
Dim twitrName = m_node.ChildNodes.Item("name").InnerText
Dim twitText = m_node.ChildNodes.Item("text").InnerText
Dim twitPic = m_node.ChildNodes.Item("profile_image_url").InnerText
Dim twitTime = m_node.ChildNodes.Item("created_at").InnerText
开发者_开发知识库 twitPic = twitPic.Replace("normal", "mini")
twitProLink = "http://www.twitter.com/" & twitName
'Do Stuff with variables
Next
Ok So I should have paid more attention to the error and Visual Studio. Item retrieves a node at the given index as an integer. I was searching for the string name so I just counted the Items from 0 and used the number that item was looking for... see the code below.
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_xmld = New XmlDocument()
m_xmld.Load("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=timatwerked&count=4&include_rts=true")
m_nodelist = m_xmld.SelectNodes("/statuses/status")
For Each m_node In m_nodelist
Dim twitName = m_node.ChildNodes.Item(11).ChildNodes.Item(2).InnerText
Dim twitrName = m_node.ChildNodes.Item(11).ChildNodes.Item(1).InnerText
Dim twitText = m_node.ChildNodes.Item(2).InnerText
Dim twitPic = m_node.ChildNodes.Item(11).ChildNodes.Item(5).InnerText
Dim twitTime = m_node.ChildNodes.Item(0).InnerText
twitPic = twitPic.Replace("normal", "mini")
twitProLink = "http://www.twitter.com/" & twitName
'Do stuff with variables
Next
Now the only problem which will be a simple fix is a retweet doesn't display correctly because the nodes are different so I'll just add a If then to see if it is a retweet then adjust the nodes accordingly. Thanks again.
The solution you illustrated is not a good solution, because your code will break if the order of the nodes ever changes. The technique you have illustrated is referred to as "magic numbers". A better way to to use XPath to parse the xml object. That way, the order of the nodes is irrelevant.
I think you would need
Dim twitName = m_node.ChildNodes.Item("user").ChildNodes.Item("screen_name").InnerText
and similar stuff for retrieving other elements.
Try:
For Each m_node In m_nodelist
Dim twitName = m_node.ChildNodes.Item("user").ChildNodes.Item("screen_name").Value
'OtherNodes
'Do stuff with variables
Next
InnerText assumes that the item you set or get has children. Value only grabs the Value of the Item with no children assumed, and as BalaR Stated refer to the item by name, so if the twitter API ever updates your index pointer will be recalculated.
精彩评论