Make your own free website on Tripod.com
         

XML and VB. A simple guide.

This article is an introduction to XML and XML programming basics with VB. So if you are an expert in that area, you might want to look at other articles.

INTRO TO XML

Well, you might have started to hear more and more about XML every day. What is XML? It's a standard by which people agreed to describe data. Many things could be described using XML. For example, assume some sort of database server which provides you with the information about your friends and their phone numbers. Let's say you asked the server to show you info of all your friends who are older than 20. The result in XML would be:
                                <Friends>
                                        <Friend>
                                                <Name>John Cusack </Name>
                                                <Phone>555-343-8234</Phone>
                                        </Friend>
                                        <Friend>
                                                <Name>Melissa Earnhardt</Name>
                                                <Phone>555-444-8732</Phone>
                                        </Friend>    
                                </Friends>

                                Example 1

XML has the same structure as HTML documents. The thing to notice about XML is that it has to be well written meaning if there is a starting tag <Friend> there must be an ending tag </Friend>. Now you might ask why would you want to use XML. The answer lies in XML's simple ability to describe data, and also, XML is a plain text and doesn't take up a lot of memory (thus very useful in server communications). XML is winning approval in many areas such as databases, websites, client/server communications.

XML in VB

If you are building an application that uses XML in any way, you app must contain a XML parser. You will use XML parser to write and read XML strings. In industry, there are two types of XML parsers available. One type is based on Document Object Model (DOM) and another is based on Simple API for XML (SAX). DOM parsers have rich set of functions to work on XML documents, making it easy to examine XML documents structure and data inside. One big drawback of DOM parser is that with the increasing size of the XML document, the memory used by the DOM parser is increasing too. SAX parser takes less memory but is harder to use.
In this article, I'll introduce XML programming with Microsoft's DOM parser 3.0 (Example should work with earlier versions). This parser is available for download from Microsoft's website. When DOM parser parses the XML document it creates a tree structure containing the document. As all regular trees (in computer science), DOM tree has a root element, (<Friends> in Example 1 for example). From looking at Example 1, you might also notice that the root has two children: two <Friend> nodes. Each <Friend> node has children of its own: <Name> and <Phone>. Make sure you are familiar with the trees a little bit, that makes DOM programming quite a bit easier.

XML Programming basics

Creating XML
Let's see what code do we need to build XML string shown above in INTRO section. In the following code section, first we create the instance of DOMDocument class that we will call doc. This class is holding your XML tree structure until you are ready to get the XML string out of it. Once you have the instance of that class, a root node is created by using doc.appendChild() method that takes a node as an argument. Once you have a root node, you can attach other nodes (it's children) by calling doc.childNodes(0).appendChild() methods. childNodes(0) indicate that the root element will get nodes attached. You can create a node by itself (for example <Friend> node), and attach it later, like I did for <Friend>, <Name> and <Phone> nodes. Also notice, how value of each node is set, by setting text property to the value you want that node to have. After you are all done with attaching nodes, you get XML version by calling doc.xml. This returns XML string. Notice I have formed only the first part of XML document presented in Example 1, you should try doing the other part for your own practise:

Code (Listing 1):
    Private Sub cmdFormXML_Click()
    Dim doc As DOMDocument
    Dim node As IXMLDOMNode
    Dim namenode As IXMLDOMNode

    Set doc = New DOMDocument 'initializing DOMDocument

    Set node = doc.createNode(NODE_ELEMENT, "Friends", "")
    doc.appendChild node 'attach root node
    Set node = doc.createNode(NODE_ELEMENT, "Friend", "") 'create node Friend
    doc.childNodes(0).appendChild node
    Set namenode = doc.createNode(NODE_ELEMENT, "Name", "")
    namenode.Text = "John Cusack"
    doc.childNodes(0).childNodes(0).appendChild namenode
    Set namenode = doc.createNode(NODE_ELEMENT, "Phone", "")
    namenode.Text = "5553438234"
    doc.childNodes(0).childNodes(0).appendChild namenode

    Text1.Text = doc.xml
    ReadXML (doc.xml)        'calling sub shown in the next section
    Set doc = Nothing
End Sub


Reading XML
With DOM parsers (as well as SAX) you can read XML from the file or a string. If you want to read from the string use DOMDocuments loadXML method and provide XML string. If you want to read xml file, use load method and provide a path to the file containing XML document.

(Lising 2):
Public Sub ReadXML(ByVal xmlstr As String)

Dim doc As DOMDocument
Dim nodelist As IXMLDOMNodeList
Dim node As IXMLDOMNode

Set doc = New DOMDocument
doc.loadXML xmlstr 'load xml string

'output all Names:
Set nodelist = doc.getElementsByTagName("Name")
MsgBox nodelist.length
For Each node In nodelist
MsgBox "Node Name: " + node.nodeName
MsgBox "Node value: " + node.Text
Next node

Set doc = Nothing
Set nodelist = Nothing
Set node = Nothing

End Sub

Some Comments
As you can see, the basic XML programming is very easy. Easier than you thought. But this is just basics, and there is much more to learn. We will try to provide more information on XML and VB in the future articles, and till then let's hope this was a useful introduction into XML world.

Topics - Home

Written By Laimonas Simutis. 2002. laijerrad@yahoo.com