Published on Friday, May 18th, 2007 at 6:21 pm

Posting XML from Visual Basic 6 is pretty straightforward.  One would think.  But like many development tasks which you'd expect to take an hour or two, this one turned into a couple of days.

First of all, How are you going to get the XML across the wire ?  To answer this the easiest method I discovered was to use the MSXML object, since I have already been using it to create my XML document.

But then which version is best ? There are 6 to choose from ! To answer that I would ask you if plan on distributing your application and dont mind also taking care to include the MSXML msi installer with your deployment. For my purposes I want to make the deployment as easy as possibly. You can pretty much rely on MSXML v3 being present on a target machine. If you dont mind deploying an MSXML msi, then go for version 6.  So I went with version 3.

So far so good, I created my VB code to use MSXML3 to create the document and perform a HTTP post to my servlet which read it in without difficulty.

However the document being sent was 20k in length. That size might not be of significance to you but for me, this action maybe repeated up to 30 times, from many different machines so optimising bandwidth use was also a priority.

The immediate solution is to compress the data.  Searching the web there are many compression components available for VB. However I dont want to pay for something and really just need to zip the data so I can unzip it from my servlet.  I used the ZLIB library from  Very easy to use, simply reference zlib.dll from your project and you can either compress a string or a byte array which suits us perfectly.  

Using ZLIB I took the original XML Documents xml source and first converted it to a byte array. Then using the compressData method from the ZLIB library, compressed the data. This took my 15k document down to 2k.

So now I've got the compressed data,  I can just post this ? No.  It must be encoded first.  Without going into why data crossing across the internet must be converted to 7 bit ASCII for succesful transmission, just accept that it does.

What encoding method should we use ?    I repeatedly was having problems at this stage. You can specify that a XML node is intended to contain encoded binary by setting its data type to binary.base64.  I had problems here because even though I did this, I was trying to insert a string representation of the compressed data into the nodes content.  This would raise an error from MSXML.  You HAVE to insert a byte array.

These problems led me to trying various other base64 encoding solutions, each giving different results !

So my working solution was to create a New XML Document containing a single element (I called it Data). The data type definition for data is set to binary.base64.  As soon as I insert my compressed byte array into the "Data" node, it is automatically encoded to base64 so it can be transmitted successfully.

 But not quite. I was still having issues..  When my servlet decodes, then decompress the data… Its all in unicode !  So I was unable to recreate an XML document on the servlet side.

 It turned out the routine I used to convert the XML data as a String to a Byte array is creating unicode bytes.. Luckily I found an alternative routine which gives the option to use ASCII/Unicode.

 Once that was sorted everything works great..   & Also across HTTPS. So my XML transmission is fast and secure.

 Source code examples on request.

Be Sociable, Share!

Related Posts

3 Responses to “Posting large xml documents from VB6 to a Java servlet.”

  1. Hey!

    Can you please send some source code examples doing the stuff you are describing here?


  2. Can you please send me your sources. I have difficulty with encoding base64 in vb6 and decoding in java: invalid stream header: 50532831
    error: Parse error occurred – null

  3. On the vb side, I have two functions which I used, a sendFileToURL(filename,url) which performs the actual and a function which reads the entire file into a string. These are below, these should help you send the data to your servlet, which then you should able to access using a BufferedReader and request.getReader(). Please let me know if you still have further issues.

    Private Function FileText(filename$) As String
        Dim handle As Integer
        handle = FreeFile
        Open filename$ For Input As #handle
        FileText = Input$(LOF(handle), handle)
        Close #handle
    End Function
    Public Sub sendFileToUrl(filename As String, url As String)
        On Local Error GoTo errors
        Dim xml As New MSXML2.XMLHTTP
        Dim dataToSend As String
        'Set xml = CreateObject("MSXML2.ServerXMLHTTP")
        Dim doc As New MSXML2.DOMDocument
        doc.preserveWhiteSpace = True
        doc.async = False
        If Dir(filename) <> "" Then
            doc.Load filename
            dataToSend = FileText(filename)
   "POST", url, False, Empty, Empty
            xml.setRequestHeader "Content-Type", "text/xml"
            xml.send doc.xml 'dataToSend
            statusCode = xml.Status
            If statusCode = 200 And Not IsNull(xml.responseXML) Then
                responseText = xml.responseXML.xml
                responseText = xml.responseText
            End If
            Debug.Print statusCode
            Debug.Print responseText
        End If
        If Err.Number > 0 Then
            If showErrorMessage Then MsgBox ("Error " + Str(Err) + " : " + Error$(Err))
        End If
        Set xml = Nothing
    End Sub

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

About Me

Welcome to my blog. Here you'll find mainly work related content, a suppository for all my notes which otherwise would end up on a post it note.