We use the XMLReader object of .NET for reading and parsing the XML
data returned by the web service.
The final result looks as follows:
GuiXT Script
GuiXT
// build up variables with city names
Clear V[myweather.cell.*]
// Cities
Set V[myweather.cell.city.1] "Berlin,DE"
Set V[myweather.cell.city.2] "Hamburg,DE"
Set V[myweather.cell.city.3] "Dortmund,DE"
Set V[myweather.cell.city.4] "Heidelberg,DE"
Set V[myweather.cell.city.5] "Roma,IT"
Set V[myweather.cell.city.6] "Sydney,AU"
Set V[myweather.cell.city.7] "Los Angeles,US"
Set V[myweather.cell.city.8] "New York,US"
Set V[myweather.cell.city.9] "Las Vegas,US"
Set V[myweather.cell.city.10] "Quebec,CA"
// read weather data for each city
Set V[k] 1
label next_city
if V[myweather.cell.city.&V[k]]
// reset variables
Clear V[response.*]
CallVB "utilities.connect.weather" "&V[myweather.cell.city.&V[k]]" "response"
// weather data found?
if V[response.temperature.value]
Set V[myweather.cell.temperature.&V[k]] "&V[response.temperature.value]°"
Set V[myweather.cell.clouds.&V[k]] "&V[response.clouds.name]"
Set V[myweather.cell.wind.&V[k]] _
"&V[response.speed.name], &V[response.speed.value] km/h"
Set V[myweather.cell.humidity.&V[k]] "&V[response.humidity.value]%"
endif
set V[k] &V[k] + 1
goto next_city
endif
// table row count
Set V[rows] &V[k] - 1
Table (2,2) (15,80) title="Readings from &V[today_d.m.y h:m]h" _
name="myweather" rows="&V[rows]" fixedColumns=5
Column "City" -readOnly size=16 name="city"
Column "Temp." -readOnly size=8 name="temperature"
Column "Clouds" -readOnly size=16 name="clouds"
Column "Humidity" -readOnly size=8 name="humidity"
Column "Wind" size=26 name="wind" -readOnly
// add button to transfer new readings
Pushbutton (15,2) "Transfer New Readings" "/0" size=(1,28)
VB.NET function
VB.NET
Public Class connect
Private guixt As New guinet.guixt
Public Sub weather(city As String, varname As String)
' build URL according to open weather API, for the given city
Dim url As String = _
"http://api.openweathermap.org/data/2.5/weather?q=" & _
city & _
"&APPID=15c03608ece9fc377a13a6cd2cxxxxxx&mode=xml" & _
"&units=metric&lang=en"
' API returns XML stream with weather data
Dim XMLReader As New Xml.XmlTextReader(url)
With XMLReader
Do While .Read
Select Case .NodeType
Case Xml.XmlNodeType.Element
Dim xname As String = .LocalName
If .AttributeCount > 0 Then
While .MoveToNextAttribute ' next attribute
' set GuiXT variable
guixt.SetVariable _
(varname & "." & xname & _
"." & .LocalName, .Value)
End While
End If
End Select
Loop
.Close()
End With
End Sub
End Class