devxlogo

Quick-Start XML

Quick-Start XML

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:

					Buick			LeSabre			1987							Chevrolet			Venture			1999			

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.DOMDocumentDim nList As MSXML.IXMLDOMNodeListDim 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.xmlNext

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.xmlNext

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist