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 : Page 2

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

Third way: the SAX one

Even if generically known as a good interface to read data, SAX (at least in the Microsoft XML Parser 3.0) has also a set of interfaces to write data. This is defined using the MXXMLWriter object in the MSXML object model and, if you search for it in the latest MSDN Library, you will find only 19 articles speaking of it.



From a programming point of view, the same code of above using DOM is created using MXXMLWriter with the following code:

    Dim lSAXReader As New SAXXMLReader

    Dim lSAXWriter As New MXXMLWriter

    'We need these variables for typecasting the writer

    Dim lSAXContentHandler As IVBSAXContentHandler

    'That is just a helper

    Dim lSAXAttributes As New SAXAttributes

    'Set handler variables to the writer so it implements the interfaces

    Set lSAXContentHandler = lSAXWriter

    'do not include XML declaration, so that the file is like the others      

    lSAXWriter.omitXMLDeclaration = True

    ' Manually call necessary events to generate the XML file

    lSAXContentHandler.startDocument

             

    lSAXAttributes.Clear

    'Create Root

    lSAXContentHandler.startElement "", "", "ROOT", lSAXAttributes

        'Create node

        lSAXContentHandler.startElement "", "", "LEVEL1", lSAXAttributes

                ' Create and populate elements

                lSAXContentHandler.startElement "", "", "VALUE11", _

lSAXAttributes

                lSAXContentHandler.characters "11"

                lSAXContentHandler.endElement "", "", "VALUE11"

        lSAXContentHandler.startElement "", "", "LEVEL2", lSAXAttributes

       

                lSAXContentHandler.startElement "", "", "VALUE21", _

lSAXAttributes

                lSAXContentHandler.characters "11"

                lSAXContentHandler.endElement "", "", "VALUE21"

        lSAXContentHandler.endElement "", "", "LEVEL2"

        lSAXContentHandler.endElement "", "", "LEVEL1"

    lSAXContentHandler.endElement "", "", "ROOT"

It’s a little more verbose than DOM but is still pretty readable and, with some more work, can be easily generalized to render different data.

Fourth way: The Fast String one

At this point of my exploration I wondered some time about the real need of some kind of object model when creating an XML file. After all what we’re doing is creating a simple ASCII text file, hence maybe all the overhead of keeping in memory a complex structure with nodes and elements is not really needed. Problem is that string manipulation from Visual Basic is really slow, and suddenly I remembered that our guest Francesco Balena some time ago wrote a very performance wise string class for Visual Basic Programmer’s Journal. After some digging around I got the CString.zip file that included the CString class, a class that, using some Windows API, improves what is described in tip bank.

Here is the code that creates a simple XML like that shown in the program picture, the only point to note is the SetBufferSize instruction that allocate the space to store the string. Here is set to a very high value (20 Mb), but the best results are obtained if you declare to a size comparable to that of the document you’re creating (if you know the structure of your document, it is possible to calculate its size).

    Dim lCS As New CString

   

    lCS.SetBufferSize(20000000, True)

    lCS.Append "<ROOT>"

        lCS.Append "<LEVEL1>"

            lCS.Append "<VALUE11>11</VALUE11""

        lCS.Append "<LEVEL2>"

            lCS.Append "<VALUE21>11</VALUE21>"

        lCS.Append "</LEVEL2></LEVEL1>"

    lCS.Append "</ROOT>"

It’s very compact, but also difficult to generalize. Beside that, I’ve not considered all the features (like character escaping or character sets support) that would make creating a well-formed XML file a much more complex task.



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