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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Yahoo weather feed XML to text file 1

Status
Not open for further replies.
Dec 6, 2001
67
0
0
JM
I am trying to setup an automated weather update to show on some plasma screens.

I am able to use vbscript to query the yahoo weather service; my only problem is how to parse the XML that is downloaded. I need to get some text strings out of the XML in a certain format so that the application will know how to update based on each line of the text file,ie,:

Line1 (Place the weather is for)
Line2 (Weather condition ie, "Cloudy")
Line3 (Current Temp)
Line4 (High Temp)
Line5 (Low Temp)

etc

This is my code (which queries the XML and gets one big file):

' Retrieve the information over the web, using the WinHttp object

Dim WinHttpReq
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim sTemp

' Use the URL of the text file you wish to retrieve:
WinHttpReq.Open "GET", " False

WinHttpReq.Send
If (WinHttpReq.Status = 200) Then
sTemp = WinHttpReq.ResponseText
' Replace any carriage returns and linefeeds with spaces
sTemp = Replace(sTemp, vbCr, " ")
sTemp = Replace(sTemp, vbLf, " ")

WebText = sTemp
' msgbox(webtext)

'Create the 'weather' file
Set objFile = objFSO.OpenTextFile("C:\Weather\Weather.txt", 8, True)
objFile.WriteLine sTemp
objFile.Close
Set objFile = Noth

Else
' Failed -- return empty string
WebText = ""
End If

How can I get it to do what I want? How do you basically parse the XML to get a file without all those markup characters?

This is the format of the file I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather=" xmlns:geo=" <channel>
<title>Yahoo! Weather - Road Town, VI</title>
<link> <description>Yahoo! Weather for Road Town, VI</description>
<language>en-us</language>
<lastBuildDate>Fri, 23 Feb 2007 10:53 am LST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Road Town" region="" country="VI" />
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph" />
<yweather:wind chill="82" direction="140" speed="13" />
<yweather:atmosphere humidity="62" visibility="1609" pressure="30.02" rising="0" />
<yweather:astronomy sunrise="6:43 am" sunset="6:24 pm" />
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link> <url> </image>
<item>
<title>Conditions for Road Town, VI at 10:53 am LST</title>
<geo:lat>18.43</geo:lat>
<geo:long>-64.63</geo:long>
<link> <pubDate>Fri, 23 Feb 2007 10:53 am LST</pubDate>
<yweather:condition text="Fair" code="34" temp="82" date="Fri, 23 Feb 2007 10:53 am LST" />
<description><![CDATA[
<img src=" /><br />
<b>Current Conditions:</b><br />
Fair, 82 F<BR /><BR />
<b>Forecast:</b><BR />
Fri - Scattered Showers. High: 83 Low: 75<br />
Sat - PM Showers. High: 84 Low: 76<br />
<br />
<a href=" Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]></description>
<yweather:forecast day="Fri" date="23 Feb 2007" low="75" high="83" text="Scattered Showers" code="39" />
<yweather:forecast day="Sat" date="24 Feb 2007" low="76" high="84" text="PM Showers" code="39" />
<guid isPermaLink="false">VIXX0001_2007_02_23_10_53_LST</guid>
</item>
</channel>
</rss>
<!-- p3.weather.re3.yahoo.com uncompressed/chunked Fri Feb 23 07:12:02 PST 2007 -->

I want a file like:

Road Town
Current conditions as of 4:53 pm
Fair
83
29.96 in and rising
62%
10 mi
67
6:43
6:24



Can anyone point me in the right direction? I do not really understand VB much less VBscript....
 
XML is not a strength of mine...I've just played with it a little. See if this works for you. If it works, you should be able to see the relationship with the xml file and be able to update it to your needs.

Code:
Option Explicit
'On Error Resume Next

Dim objXML, strXMLFile, weatherFor, weatherCondition, currTemp, highTemp, lowTemp

strXMLFile = "C:\Temp\test.xml"

Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = "False"
objXML.load(strXMLFile)

weatherFor = objXML.getElementsByTagName("yweather:location").item(0).attributes.getNamedItem("city").value
weatherCondition = objXML.getElementsByTagName("yweather:condition").item(0).attributes.getNamedItem("text").value
currTemp = objXML.getElementsByTagName("yweather:condition").item(0).attributes.getNamedItem("temp").value
highTemp = objXML.getElementsByTagName("yweather:forecast").item(0).attributes.getNamedItem("high").value
lowTemp = objXML.getElementsByTagName("yweather:forecast").item(0).attributes.getNamedItem("low").value

WScript.Echo weatherFor
WScript.Echo weatherCondition
WScript.Echo currTemp
WScript.Echo highTemp
WScript.Echo lowTemp

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Here is a good reference for XMLDOM
If what I posted does not work for you, I'm sure tsuji or one of the other knowledgeable members here will provide some guidance and optimal way to retrieve the info you're looking for.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
This is the ticket. Now I can see exactly how to get the other elements that I need from the XML file. I have been searching all over the net and then I remembered Tek-Tips. Thanks a lot. Big ups!(+ star)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top