Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating an XML Document: The Way of Speed

There are many ways to create an XML doc from inside a VB application. For example, you can use string concatenation, or the Xml Dom parser, or follow the SAX approach. Each one has its strengths and weeknesses, but did you ever ask yourself which is the fastest? The author of this article did it, and can now share his - somewhat surprising - results.


advertisement


If you search the Web and the various articles in magazines and MSDN you can find a lot of documentation about the fastest way of reading an XML document. Speaking of Microsoft XML Parser release 3.0, one of the most commonly discussed issues is whether use the DOM parser or the SAX one, with an almost universal convention that SAX is faster when you just need to read data, while DOM is the way to go if you need to modify and navigate them. The most obvious metaphor is that SAX is like a read only/forward only cursor for a database, while DOM is a full updateable and navigable cursor. If you’re experienced in using database you already know the differences in performance between the two ways of accessing a database.

But, speaking of writing XML data, I was unable to find references to this performance issue.



So I started experimenting and I’ve found some impressive data and some insights about how creating XML document in the fastest way that are not so clearly documented or understood.

All the samples provided in this document are made using a Windows 2000 Professional notebook, with Visual Basic 6.0 SP5 and MS XML Parser 3.0 SP1 installed.

I started with a simple idea: simulate a long document (we’ll see that the performance improvements starts to become impressive when the number of nodes becomes large) with at least one Father-Child structure and use some different ways to manipulate data.

The VBP project provided allows you to input three parameters: how many nodes are written as fathers (with elements named LEVEL1), how many nodes are written as children (with elements named LEVEL2) and how long the text of the element must be.

First way: the long one

The first option is to use VB to create the XML string, using just the VB & operator, a very slow and somewhat deprecated option.

We all agree that this is the longest running option and, because of the way VB handles strings, it’s very slow. It’s so slow that, because of the time it takes to create XML document this way, I have not tested it, it was way too long!

Second way: the DOM one

DOM is one of the two mains API that you can use when working with XML. It’s very elegant, very powerful and also (as we’ll see) efficient. The code to create a document like the one described above (slightly different from what it’s included in the VBP to improve readability) looks like this:

    Set lDOMDoc = CreateObject("MSXML2.DOMDocument.3.0")

    'Create root

    Set lDOMRootNode = lDOMDoc.createElement("ROOT")

    lDOMDoc.appendChild lDOMRootNode

        ' Create node

        Set lDOMLev1Node = lDOMDoc.createElement("LEVEL1")

        lDOMRootNode.appendChild lDOMLev1Node

            ' Create element

            Set lValueElement = lDOMDoc.createElement("VALUE11")

            lDOMLev1Node.appendChild lValueElement

            ' Set its value

            lDOMLev1Node.lastChild.Text = "11"

        ' Same for other nodes       

        Set lDOMLev2Node = lDOMDoc.createElement("LEVEL2")

        lDOMLev1Node.appendChild lDOMLev2Node

            Set lValueElement = lDOMDoc.createElement("VALUE21")

            lDOMLev2Node.appendChild lValueElement

            lDOMLev2Node.lastChild.Text = "11"

As you can see, the code is very readable; you create your DOM document, append nodes to it, set their value and so on. Clear, concise and can be easily mapped, for example, with the code that loops through one or more recordsets and be also generalized.



Comment and Contribute

 

 

 

 

 


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

 

 

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