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
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.
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
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!
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 =
Set lDOMRootNode =
' Create node
lDOMLev1Node = lDOMDoc.createElement("LEVEL1")
Set lValueElement =
' Set its value
lDOMLev1Node.lastChild.Text = "11"
' Same for other nodes
lDOMLev2Node = lDOMDoc.createElement("LEVEL2")
Set 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.