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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS