Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Yahoo Weather RSS parsing problem 1

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
Ok, I can read the RSS feed with classic ASP and display the entire feed, but I just want to display the actual temperature. Here is my code:

Code:
call getWeather(1)
 
Sub getWeather(howManyResults)
myRSSfile = "[URL unfurl="true"]http://weather.yahooapis.com/forecastrss?p=98119"[/URL]
 
Set xmlHttp = Server.CreateObject("MSXML2.XMLHTTP.4.0")
xmlHttp.Open "Get", myRSSfile, false
xmlHttp.Send()
myXML = xmlHttp.ResponseText
 
Set xmlResponse = Server.CreateObject("MSXML2.DomDocument.4.0")
xmlResponse.async = false
xmlResponse.LoadXml(myXML)
Set xmlHttp = Nothing
 
Set objLst = xmlResponse.getElementsByTagName("item")
Set xmlResponse = Nothing
 
intNoOfHeadlines = objLst.length -1
 
For i = 0 To (intNoOfHeadlines)
Set     objHdl = objLst.item(i)
 
for each child in objHdl.childNodes
Select case lcase(child.nodeName)
    case "title"
          title = child.text
    case "link"
          link = child.text
    case "description"
          description = child.text
End Select
next
  kk = kk+1
     if kk < howManyresults+1 then
    Response.Write "<br /><a href=""" & link & """>" & title & "</a> <br 
/>" & description
 
    end if
 
Next
End sub

and here is what the feed looks like

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="[URL unfurl="true"]http://weather.yahooapis.com/ns/rss/1.0"[/URL]
 xmlns:geo="[URL unfurl="true"]http://www.w3.org/2003/01/geo/wgs84_pos#">[/URL]
<channel>
   <title>Yahoo! Weather - Sunnyvale, CA</title>
   <link>[URL unfurl="true"]http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/[/URL]
   *[URL unfurl="true"]http://weather.yahoo.com/forecast/94089_f.html</link>[/URL]
   <description>Yahoo! Weather for Sunnyvale, CA</description>
   <language>en-us</language>
   <lastBuildDate>Tue, 29 Nov 2005 3:56 pm PST</lastBuildDate>
   <ttl>60</ttl>
   <yweather:location city="Sunnyvale" region="CA" country="US"></yweather:location>
   <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"></yweather:units>
   <yweather:wind chill="57" direction="350" speed="7"></yweather:wind>
   <yweather:atmosphere humidity="93" visibility="1609" pressure="30.12" rising="0"></yweather:atmosphere>
   <yweather:astronomy sunrise="7:02 am" sunset="4:51 pm"></yweather:astronomy>
   <image>
      <title>Yahoo! Weather</title>
      <width>142</width>
      <height>18</height>
      <link>[URL unfurl="true"]http://weather.yahoo.com/</link>[/URL]
      <url>[URL unfurl="true"]http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>[/URL]
   </image>
   <item>
      <title>Conditions for Sunnyvale, CA at 3:56 pm PST</title>
      <geo:lat>37.39</geo:lat>
      <geo:long>-122.03</geo:long>
      <link>[URL unfurl="true"]http://us.rd.yahoo.com/dailynews/rss/weather/[/URL]
       Sunnyvale__CA/*
       [URL unfurl="true"]http://weather.yahoo.com/[/URL] forecast/94089_f.html
      </link>
      <pubDate>Tue, 29 Nov 2005 3:56 pm PST</pubDate>
      <yweather:condition text="Mostly Cloudy" code="26" temp="57" date="Tue, 29 Nov 2005 3:56
          pm PST"></yweather:condition>
      <description><![CDATA[
 <img src="[URL unfurl="true"]http://l.yimg.com/a/i/us/we/52/26.gif"[/URL] /><br />
 <b>Current Conditions:</b><br />
 Mostly Cloudy, 57 F<p />
 <b>Forecast:</b><BR />
  Tue - Mostly Cloudy. High: 62 Low: 45<br />
  Wed - Mostly Cloudy. High: 60 Low: 52<br />
  Thu - Rain. High: 61 Low: 46<br />
<br />
<a href="[URL unfurl="true"]http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/94089_f.html">Full[/URL] Forecast at Yahoo! Weather</a><BR/>
 (provided by The Weather Channel)<br/>]]>
      </description>
      <yweather:forecast day="Tue" date="29 Nov 2005" low="45" high="62" text="Mostly Cloudy"
          code="27"></yweather:forecast>
      <yweather:forecast day="Wed" date="30 Nov 2005" low="52" high="60" text="Mostly Cloudy"
          code="28"></yweather:forecast>
      <guid isPermaLink="false">94089_2005_11_29_15_56_PST</guid>
   </item>
</channel>
</rss>

What I can't figure out is how to just grab item/pubdate and item/yweather:condition/temp. If I try to change
Code:
Set objLst = xmlResponse.getElementsByTagName("item")
to specify the element, I can't get down to the temp. Just like CSS, when I thought I had a good grasp, I want astray... Any help out there?

Willie
 
So, if I change my sub to this:
Code:
Sub getWeather(howManyResults)
myRSSfile = "[URL unfurl="true"]http://weather.yahooapis.com/forecastrss?p=98119"[/URL]
 
Set xmlHttp = Server.CreateObject("MSXML2.XMLHTTP.4.0")
xmlHttp.Open "Get", myRSSfile, false
xmlHttp.Send()
myXML = xmlHttp.ResponseText
 
Set xmlResponse = Server.CreateObject("MSXML2.DomDocument.4.0")
xmlResponse.async = false
xmlResponse.LoadXml(myXML)
Set xmlHttp = Nothing
 
Dim TDate : TDate = xmlResponse.getElementsByTagName("pubDate").item(0).text
Dim TTemp : TTemp = xmlResponse.selectSingleNode("item/yweather:condition").Attributes.
GetNamedItem("temp").text

Set xmlResponse = Nothing
 
 
    Response.Write TDate & "<br>"
    Response.Write TTemp
 
 
End sub

I get this error
Code:
msxml4.dll error '80004005'

Reference to undeclared namespace prefix: 'yweather'.

/depts/facman/getyahooweather2.asp, line 20
Where line 20 is Dim TTemp...

I don't understand why (nor how) I would have to add a namespace to a standalone RSS feed just so I can get to the node attributes. Anyone?

Willie
 
[1] >What I can't figure out is how to just grab item/pubdate and item/yweather:condition/temp.
If I restrict myself to the language of dom (level x) you're using, like getElementsByTagName etc..., you sure can get those nodes. (temp is an attribute, the path to it is not correct.)
[tt]
Sub getWeather(howManyResults)
[blue]'etc etc...[/blue]
for each child in objHdl.childNodes
Select case lcase(child.nodeName)
case "title"
title = child.text
case "link"
link = child.text
case "description"
description = child.text
[blue]case "pubdate"
pubdate=child.text
case "yweather:condition"
temp=child.getAttribute("temp")[/blue]

End Select
next

kk = kk+1
if kk < howManyresults+1 then
Response.Write "<br /><a href=""" & link & """>" & title & "</a> <br />" & description
[blue]Response.Write "<br />" & pubDate
Response.Write "<br />" & temp[/blue]
end if
Next
End sub
[/tt]
[1.1] I put barebone response.write, you can elaborate it.

[1.2] xml is case sensitive, hence, in principle, select case lcase(child.nodeName) is not 100% appropriate constructed in accordance with the spirit of xml. You can use the exact upper/lower case to match nodename.

[1.3] You see the hook to "yweather" in its exact prefix is needed. This is again in odd with the spirit of xml construction because the prefix is quite arbitrary. But, in the approach along the line used, that artifact has to be followed letter-to-letter. That is not satisfactory. You will see in [2], that kind of defect is remedied.

[2] If you use the approach using more proactively XPath technology, as it seems to suggest you're intending to in the second post. You can eliminate the critic as noted in [1.3]. However, the detail of the implementation is more involved.

[2.1] Before showing how, I have noted that the namespace with yweather is actually changed. Take note of that.

[2.2] As to the implementation, this is how. (I use all your lines except I have to change. For insufficient but working lines, I preserve as you are using.)
[tt]
Sub getWeather(howManyResults)
myRSSfile = "[ignore][/ignore]"

Set xmlHttp = Server.CreateObject("MSXML2.XMLHTTP.4.0")
xmlHttp.Open "Get", myRSSfile, false
xmlHttp.Send()
myXML = xmlHttp.ResponseText
Set xmlHttp = Nothing

Set xmlResponse = Server.CreateObject("MSXML2.DomDocument.4.0")
xmlResponse.async = false
xmlResponse.LoadXml(myXML)
[blue]
xmlResponse.SetProperty "SelectionNamespaces", "xmlns:x='[ignore]http://[/ignore][red]xml.weather.yahoo.com[/red][ignore]/ns/rss/1.0[/ignore]'"
xmlResponse.SetProperty "SelectionLanguage", "XPath"

For i = 0 To howManyResults
if not xmlResponse.selectSingleNode("/rss/channel/item[" & (i+1) & "]") is Nothing then
title = xmlResponse.selectSingleNode("/rss/channel/item[" & (i+1) & "]/title").text
link = xmlResponse.selectSingleNode("/rss/channel/item[" & (i+1) & "]/link").text
description = xmlResponse.selectSingleNode("/rss/channel/item[" & (i+1) & "]/description").xml
pubdate=xmlResponse.selectSingleNode("/rss/channel/item["& (i+1) & "]/pubDate").text
temp=xmlResponse.selectSingleNode("/rss/channel/item[" & (i+1) & "]/x:condition").getAttribute("temp")
Response.Write "<br /><a href=""" & link & """>" & title & "</a> <br />" & description
Response.Write "<br />" & pubDate
Response.Write "<br />" & temp
else
exit for
end if
Next
set xmlResponse=nothing
[/blue]
End sub
[/tt]
[2.3] The prefix is then quite arbitrary. I use x to highlight the arbitrariness.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top