yahoo OPEN api测试之weather feed
昨天写了个google的feed api应用,突然对rss蛮感兴趣的,天生比较好奇,以前都是做windows程序感觉进入互联网领域,象是走进另外一扇大门,更加有趣,更加丰富多彩.先展示一下结果http://blog.运维网.com/attachment/201003/201003101268236559171.bmp
调用feed api最主要要学习的知识是RSS读取的问题.
.NET框架提供了System.Xml.Serialization.XmlSerializer类型,为将对象序列化为XML或将XML序列化为对象提供了很大支持。
下面我们针对yahoo weather feed api,通过实例讲解一下如何读取RSS文件。
首先通过yahoo weather feed api
"http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
可以得到如下RSS文件
Yahoo! Weather - Hangzhou, CH
http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html
Yahoo! Weather for Hangzhou, CH
en-us
Fri, 27 Feb 2009 12:30 pm CST
60
Yahoo! Weather
142
18
http://weather.yahoo.com
http://l.yimg.com/a/i/us/nws/th/main_142b.gif
Conditions for Hangzhou, CH at 12:30 pm CST
30.23
120.17
http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html
Fri, 27 Feb 2009 12:30 pm CST
Current Conditions:
Rain Shower, 43 F
Forecast:
Fri - Rain. High: 42 Low: 39
Sat - Rain. High: 43 Low: 40
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>
CHXX0044_2009_02_27_12_30_CST
如上文所说.NET框架提供了System.Xml.Serialization.XmlSerializer类型,现在我们要做的就是如何利用xmlSerializer将XML序列化为对象。
对于上面的XML文件为了便于对象序列化我们会做如下替换
str = str.Replace("yweather:", "")
str = str.Replace("geo:", "")
'Dim str1 As String = """: "
Dim str1 = "isPermaLink=""false"""
str = str.Replace(str1, "")
str1 = " version=""2.0"" xmlns:yweather=""http://xml.weather.yahoo.com/ns/rss/1.0"" xmlns:geo=""http://www.w3.org/2003/01/geo/wgs84_pos#"""
str = str.Replace(str1, "")
接着我们需要按照xml的格式,将所有的节点定义为对象,并将节点的属性定义为对象的属性。
如该RSS中需要先定义rss对象
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
_
Public Class rssClass
Private _channel As channelClass
_
Public Property channel() As channelClass
Get
Return _channel
End Get
Set(ByVal value As channelClass)
_channel = value
End Set
End Property
End Class
End Class
在该对象中包含chanelClass,保持和xml的文件相同的结构
接下来我们需要定义chanelClass,代码如下:
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
Partial Class rssClass
Partial Class channelClass
Private _title As String
'''
'''The title of the feed, which includes the location city. For example "Yahoo! Weather - Sunnyvale, CA"
'''
_
Public Property title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Private _link As String
'''
'''The URL for the Yahoo! Weather page of the forecast for this location. For example http://us.rd.yahoo.com/dailynews/rss/weather/ Sunnyvale__CA/ *http://xml.weather.yahoo.com/ forecast/94089_f.html
'''
_
Public Property link() As String
Get
Return _link
End Get
Set(ByVal value As String)
_link = value
End Set
End Property
Private _language As String
'''
'''The language of the weather forecast, for example, en-us for US English.
'''
_
Public Property language() As String
Get
Return _language
End Get
Set(ByVal value As String)
_language = value
End Set
End Property
Private _description As String
'''
'''The overall description of the feed including the location, for example "Yahoo! Weather for Sunnyvale, CA"
'''
_
Public Property description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Private _lastBuildDate As String
'''
'''The last time the feed was updated. The format is in the date format defined by RFC822 Section 5, for example Mon, 256 Sep 17:25:18 -0700.
'''
_
Public Property lastBuildDate() As String
Get
Return _lastBuildDate
End Get
Set(ByVal value As String)
_lastBuildDate = value
End Set
End Property
Private _ttl As String
'''
'''Time to Live; how long in minutes this feed should be cached.
'''
_
Public Property ttl() As String
Get
Return _ttl
End Get
Set(ByVal value As String)
_ttl = value
End Set
End Property
Private _location As locationClass
'''
'''The location of this forecast.
'''
_
Public Property location() As locationClass
Get
Return _location
End Get
Set(ByVal value As locationClass)
_location = value
End Set
End Property
Private _units As unitsClass
'''
'''Units for various aspects of the forecast.
'''
_
Public Property units() As unitsClass
Get
Return _units
End Get
Set(ByVal value As unitsClass)
_units = value
End Set
End Property
Private _wind As windClass
'''
'''Forecast information about wind.
'''
_
Public Property wind() As windClass
Get
Return _wind
End Get
Set(ByVal value As windClass)
_wind = value
End Set
End Property
Private _atmosphere As atmosphereClass
'''
'''Forecast information about current atmospheric pressure, humidity, and visibility.
'''
_
Public Property atmosphere() As atmosphereClass
Get
Return _atmosphere
End Get
Set(ByVal value As atmosphereClass)
_atmosphere = value
End Set
End Property
Private _astronomy As astronomyClass
'''
'''Forecast information about current astronomical conditions.
'''
_
Public Property astronomy() As astronomyClass
Get
Return _astronomy
End Get
Set(ByVal value As astronomyClass)
_astronomy = value
End Set
End Property
Private _image As imageClass
'''
'''The image used to identify this feed
'''
_
Public Property image() As imageClass
Get
Return _image
End Get
Set(ByVal value As imageClass)
_image = value
End Set
End Property
Private _item As itemClass
'''
'''The local weather conditions and forecast for a specific location.
'''
_
Public Property item() As itemClass
Get
Return _item
End Get
Set(ByVal value As itemClass)
_item = value
End Set
End Property
End Class
End Class
End Class
在chanel中声明了他包含哪些子节点,同样我们也需要为这些子节点申明对象。
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
Partial Class rssClass
Partial Class channelClass
Public Class atmosphereClass
Private _humidity As Integer
'''
'''humidity, in percent
'''
_
Public Property humidity() As Integer
Get
Return _humidity
End Get
Set(ByVal value As Integer)
_humidity = value
End Set
End Property
Private _visibility As Double
'''
''' in the units specified by the distance attribute of the units element (mi or km). Note that the visibility is specified as the actual value * 100. For example, a visibility of 16.5 miles will be specified as 1650. A visibility of 14 kilometers will appear as 1400.
'''
_
Public Property visibility() As Double
Get
Return _visibility
End Get
Set(ByVal value As Double)
_visibility = value
End Set
End Property
Private _pressure As Double
'''
'''barometric pressure, in the units specified by the pressure attribute of the units element (in or mb).
'''
_
Public Property pressure() As Double
Get
Return _pressure
End Get
Set(ByVal value As Double)
_pressure = value
End Set
End Property
Private _rising As Integer
'''
'''state of the barometric pressure, steady = 0, rising = 1, falling = 2
'''
_
Public Property rising() As Integer
Get
Return _rising
End Get
Set(ByVal value As Integer)
_rising = value
End Set
End Property
End Class
End Class
End Class
End Class
在申明完对象之后,我们就可以将xml文件转换为相应的对象。
代码如下:
Dim doc As New System.Xml.XmlDocument()
Dim str As String = "http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
Dim c As rssClass
'Load data
doc.Load(str)
str = doc.OuterXml
'XmlSerializer could not Serialize XML with this Elements so i just replace them with Null String!
str = str.Replace("yweather:", "")
str = str.Replace("geo:", "")
'Dim str1 As String = """: "
Dim str1 = "isPermaLink=""false"""
str = str.Replace(str1, "")
str1 = " version=""2.0"" xmlns:yweather=""http://xml.weather.yahoo.com/ns/rss/1.0"" xmlns:geo=""http://www.w3.org/2003/01/geo/wgs84_pos#"""
str = str.Replace(str1, "")
Dim memoryStream As New IO.MemoryStream()
Dim d As New IO.StringReader(str)
Dim xs As New XmlSerializer(GetType(rssClass))
c = CType(xs.Deserialize(d), rssClass)
rss = c
最后用户可以根据自己的需要将对象中的属性取出,使用于相关的应用中。类似如下代码
Dim t As New YahooWeatherLib.YahooWeatherForecast("CHXX0044", "f")
Label1.Text = t.rss.channel.item.forecast.high
Label2.Text = t.rss.channel.item.forecast.low
Label5.Text = t.rss.channel.item.title
页:
[1]