Authenticated as: Anonymous (Change Credentials / Create Account)

fwaggle

By fwaggle

Posted: 08:11:51 2008-03-17

Modified: 08:16:36 2008-03-17 by fwaggle

XML parsing in Visual Basic 6 - Weather Application

I'd been toying with this idea for a while, and never really got around to doing it. Windows Vista includes a widget that gets the weather for a given location, but what if you wanted to make your own? What if you wanted to do it using a stoneage tool such as Visual Basic 6? Not to worry, it can be done.

You will need:

First thing you need to do is dig up some data you want to use. Google provides XML access to it's weather data which you can select by passing it a zip code, for example the weather for Sacramento, CA is available from http://www.google.com/ig/api?weather=95820.

Now start a VB6 project. In the Project menu, select "References" and select the newest Microsoft XML library you can find. Right click on the controls toolbox and pick components, and add the Microsoft Internet Transport Control. Drag one onto your project, I named mine inet.

Create a new textbox, I named mine debugXML, and made it multiline and stretched it out a bit so I could see the data. You'll need to declare the following:

Dim xml_document As New DOMDocument Dim xml_node As IXMLDOMNode

You can name them something a little more appropriate to your project, I just picked names out of the sky. xml_document holds the entire XML document, parsed and unparsed. xml_node is used for picking out nodes by their path (more on that later).

Now, in a timer or whatever you think is best, put the following:

' download xml from server debugXML.Text = inet.OpenURL("http://www.google.com/ig/api?weather=95820", icString) ' parse as xml xml_document.loadXML debugXML.Text

We should now be able to download and parse the XML that Google spits out. You can check xml_document.parseError to see if it's valid XML, I didn't bother for the purposes of my playing. If parseError is non-zero and you try to use any of the XML functions VB will throw an exception.

Next up, let's show the user where we're looking at the weather. We use the XML path to dig up the city tag. If you cut out all the irrelevant crap, you'll see the structure is something like:

<xml_api_reply><weather><forecast_information><city data="Sacramento, CA" />

So we can use selectSingleNode() method, passing it an XML path which will give us a node. We then drag the text property of the attribute "data" and stuff it in the form's caption:

Set xml_node = xml_document.documentElement.selectSingleNode("//xml_api_reply/weather/forecast_information/city") Form1.Caption = "Weather Report: " + xml_node.Attributes.getNamedItem("data").Text

The same exact method can be used to extract the current conditions temperature:

Set xml_node = xml_document.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions/temp_f") txtTemp.Caption = xml_node.Attributes.getNamedItem("data").Text

Going through the forecast days is a little tougher, because as far as I know XML paths won't allow you to distinguish between multiple elements who are siblings and have the same names. You'll probably have to walk through the nodes using nextNode and keep looking for the data you want. For this reason, and my own laziness, it's left as an exercize for the reader.