The Hungry Hacker's Explanation of Everything

Home » Software

Parsing XML in Visual Basic 6

20 July 2008 3 Comments

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 stone-age tool such as Visual Basic 6? Not to worry, it can be done.

You will need:

  • Visual Basic 6 (there’s probably better ways to do this in newer versions)
  • Microsoft XML (I used v6, who knows what they’re up to now)
  • Microsoft Internet Transport Control
  • Google’s Cooperation

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

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("", 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 exercise for the reader.


  • Jamie said:

    i can get this to work with normal XML site situations just fine but for some reason i cant do it with the world of warcraft armory it just hits me with html and java etc, (, right click view source from a proper webbrowser even the webbrowser control and it works fine. please help me 🙂

  • fwaggle (author) said:

    My guess is because you’re using VB6, it might be sending a weird user agent that that website is interpreting as too old to understand XML (~MSIE6 maybe?), and so it’s trying to be clever and show you HTML instead. I don’t have VB6 installed, so I can’t verify that – you could try and see if there’s a way to change the user agent header on an inet control, or perhaps just use sockets and do the HTTP request yourself. I’m not sure other than that, sorry. 🙁

  • Moe Joe said:

    Thx for the code scrub! 16 hours of searching and i found it!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar. Note: By filling out this comment form or emailing us you are signifying that you have read and agree to the terms laid out on the Contact Us page.