Blake81
Well-known member
I have this code that parses an XML feed online to get the weather for any US zip code. Because of our current weather, I just found out that if there is a sever weather advisory, there is a new node with child nodes describing the condition. I haven't worked with VB in awhile, and I was wondering how to write the code to find if a specific node exists. Here's the code I have now, before any changes to get this working.
[/COLOR][/SIZE]
The XML I want is the <swa> node, specifically the values of its child nodes. Here's what the XML looks like up through that node. Thanks for any help.
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] GetWeather([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Zip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Len(Zip) < 6 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmldocument [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument.Load([/SIZE][SIZE=2][COLOR=#800000]"http://xoap.weather.com/weather/local/"[/COLOR][/SIZE][SIZE=2] + Zip + [/SIZE][SIZE=2][COLOR=#800000]"?cc=*&dayf=5&prod=xoap&par=1017300893&key=268086eb4218acff"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNodeList
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xname [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]Xname = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/loc/dnam"[/COLOR][/SIZE][SIZE=2]).InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newxname [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Mid(Xname, 1, Xname.Length - 8)
newzipone = Mid(Zip, 1, 1)
newziptwo = Mid(Zip, 2, 1)
newzipthree = Mid(Zip, 3, 1)
newzipfour = Mid(Zip, 4, 1)
newzipfive = Mid(Zip, 5, 1)
weatherstring = [/SIZE][SIZE=2][COLOR=#800000]"Weather for "[/COLOR][/SIZE][SIZE=2] & newzipone & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newziptwo & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newzipthree & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newzipfour & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newzipfive & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newxname & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2]Xnode = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/cc"[/COLOR][/SIZE][SIZE=2])
Xnodes = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] Xnodes
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"tmp"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" Temperature: "[/COLOR][/SIZE][SIZE=2] & Xnode.InnerText() & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"flik"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" Feels Like: "[/COLOR][/SIZE][SIZE=2] + Xnode.InnerText() & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" Conditions: "[/COLOR][/SIZE][SIZE=2] & Xnode.InnerText() & [/SIZE][SIZE=2][COLOR=#800000]","
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"wind"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] XWindList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNodeList
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xwind [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
Xwind = Xnode.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/cc/wind"[/COLOR][/SIZE][SIZE=2])
XWindList = Xwind.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xwind [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] XWindList
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xwind.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WindDir [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]" Winnd: From "[/COLOR][/SIZE][SIZE=2] + Xwind.InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xwind.Name = [/SIZE][SIZE=2][COLOR=#800000]"s"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xwind.InnerText = [/SIZE][SIZE=2][COLOR=#800000]"calm"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]windspeed = [/SIZE][SIZE=2][COLOR=#800000]"Winnd is calm"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]windspeed = [/SIZE][SIZE=2][COLOR=#800000]"Winnd at "[/COLOR][/SIZE][SIZE=2] + Xwind.InnerText + [/SIZE][SIZE=2][COLOR=#800000]" miles per hour"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]windstring = windstring + [/SIZE][SIZE=2][COLOR=#800000]"at "[/COLOR][/SIZE][SIZE=2] + Xwind.InnerText + [/SIZE][SIZE=2][COLOR=#800000]" miles per hour"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xwind.Name = [/SIZE][SIZE=2][COLOR=#800000]"gust"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xwind.InnerText = [/SIZE][SIZE=2][COLOR=#800000]"N/A"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]windgust = [/SIZE][SIZE=2][COLOR=#800000]"With no gusting wind"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]windgust = [/SIZE][SIZE=2][COLOR=#800000]"Gusting to "[/COLOR][/SIZE][SIZE=2] & Xwind.InnerText & [/SIZE][SIZE=2][COLOR=#800000]"miles per hour"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & winddir & windspeed & windgust
sayweather(weatherstring)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
The XML I want is the <swa> node, specifically the values of its child nodes. Here's what the XML looks like up through that node. Thanks for any help.
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <!-- This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2005, The Weather Channel Interactive, Inc. All Rights Reserved. -->f(clean);
- <weather ver="2.0">
- <head>
<locale>en_US</locale>
<form>MEDIUM</form>
<ut>F</ut>
<ud>mi</ud>
<us>mph</us>
<up>in</up>
<ur>in</ur>
</head>
- <loc id="30117">
<dnam>Carrollton, GA (30117)</dnam>
<tm>10:15 PM</tm>
<lat>33.58</lat>
<lon>-85.1</lon>
<sunr>7:13 AM</sunr>
<suns>5:37 PM</suns>
<zone>-5</zone>
</loc>
- <swa>
- <a id="5540" uc="2">
<t>RIVER FLOOD WARNING FOR CARROLL COUNTY, COWETA COUNTY, HEARD COUNTY, GA UNTIL THU NOV 16 2006 01:15 PM EST</t>
<l>http://www.weather.com/weather/alerts?alertId=5540</l>
</a>
- <a id="5888" uc="0">
<t>WIND ADVISORY UNTIL THU NOV 16 2006 04:00 AM EST</t>
<l>http://www.weather.com/weather/alerts?alertId=5888</l>
</a>
</swa>