Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Quick-Start XML

Learn to create a very simple XML document.

o, you've been too busy earning an honest living to get into XML, and your data couldn't be more disorganized. Don't worry, because building upon the Visual Basic skills you already possess, you'll learn XML (eXtensible Markup Language) in no time, and everything will be tidy again. We'll begin right away by creating a simple XML document:

<cars> <car> <make>Buick</make> <model>LeSabre</model> <year>1987</year> </car> <car> <make>Chevrolet</make> <model>Venture</model> <year>1999</year> </car> </cars>

Save it in a file called xmlCars.xml. If you have IE5, double-click the file and it will display as a treeview in the browser. As you can see, XML data is hierarchically arranged — each element is a node, and can contain other nodes. The two basic rules you must know are that a document can only have one root element, and that all tags must have a corresponding closing tag.

Now, let's work with the XML DOM object model within VB. Open a VB project and reference Microsoft XML version 2.0 (msxml.dll). If you don't have it, you can download it from MSDN. Next, drop a textbox on a form, set its Multiline property to true, and make it large enough to display xmlCars. Before we go any further, we'll declare a couple of XML objects to work with:

Dim xml As New MSXML.DOMDocument Dim nList As MSXML.IXMLDOMNodeList Dim n As MSXML.IXMLDOMNode

The XML object model has the DOMDocument at the top and individual DOMNode objects corresponding to each one of the elements in the document. In the Form Load procedure load xmlCars:

xml.Load ("xmlCars.xml") '

Make sure to add the file path.

Now let's print the XML data in the text box (name it txt). Place a command button on the form and in its click event type:

txt.Text = xml.xml

Run it and you should see the entire XML tree. Try xml.Text and you'll see the data without the tags. To traverse the tree use the Node's ChildNodes property:

txt.Text = xml.documentElement.childNodes(0).childNodes(1).xml

This code should display LeSabre, which is the make of the first car in the document.

Besides traversing the tree, you can also return an individual node, or set of nodes using XSL (eXtensible Stylesheet Language) patterns. An XSL pattern is similar to a file path with some additional operators. For example, to select the Buick from xmlCars, you would use the following pattern:

Set n = xml.selectSingleNode("/cars/car/make[text()='Buick']")

First, you set the path of the node that you want to return, and then place the filter criteria in brackets. You can place a filter anywhere along the path. Note that we used the SelectSingleNode method to return the first matching node. We can also use the SelectNodes method that returns multiple nodes in a NodeList object:

Set nList = xml.selectNodes("/cars/car[year]") For Each n In nList txt.Text = txt.Text & vbCrLf & n.xml Next

Let's try one more method. The xmlDocument's getElementsByTagName method allows you to get any element within a document, regardless of its level in the hierarchy:

Set nList = xml.documentElement.getElementsByTagName("make") For Each n In nList txt.Text = txt.Text & vbCrLf & n.xml Next

This article should get you started working with XML and XSL. For more in-depth information on this subject check out: http://www.msdn.microsoft.com/xml.

Michael Gellis works at Flash Creative Management (a Microsoft Partner) as a Visual Basic developer. He is a Microsoft Certified Professional in VB5 and VB6 and is co-writing a book for Sybex on VB 6.0 and distributed applications. You can reach him here.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date