BizTalk Utilities CV ,   Jobs ,   Code library  
 
 
Page 2 of 2

 

Previous Page 

XML and Binary Data, cont.

An Example: Signature image as part of a XML document

It's now time to look at an example and see how the binary data actually gets transmitted as part of XML document. In this example, we have:

  • A Microsoft Access database called "Accounts.mdb" which has only one table with the name "AcctTable". This table has only one record with Signature field having a binary GIF image data.  We are going to demonstrate how to pass this gif in the database using XML.
  • An ASP File, getAcctDetails.asp, which connects to Accounts database and generates the XML document from the RecordSet. In this ASP file, you'll see how we are converting binary data into base64 encoding.
  • A Visual Basic Project, XMLBin.vbp, which loads the XML file having base64 encoded binary image and saves that image onto disk as a GIF file. Here we'll learn how to decode base64 data back to binary.

Here's how the Microsoft Access database record looks:

Sample database
Figure 1: Sample database, note the Signature field having binary data


Let's have a closer look at the ASP file, getAcctDetails.asp. This ASP file connects to the database and generates XML for the record using the msxml parser parser.

Here is part of the ASP file:

	Set connDB = Server.CreateObject("ADODB.Connection")
	connDB.Provider = "Microsoft.Jet.OLEDB.4.0"
	connDB.Open "d:\xmltest\Accounts.mdb"

	SQLStmt = "select * from AcctTable where Number = " & sAccountNumber

	Set rsResult = connDB.Execute(SQLStmt)

	Set xmlDOMDocument = Server.CreateObject("MSXML.DOMDocument")
	xmlDOMDocument.loadXML ""

	Set oRoot = xmlDOMDocument.createElement("BANKACCOUNT")
	Set xmlDOMDocument.documentElement = oRoot
	
	if not rsResult.EOF then
	

        Set oElement = xmlDOMDocument.createElement("NUMBER")
        oRoot.appendChild oElement
        oElement.dataType = "i2"
        oElement.nodeTypedValue = ReplaceXMLSpecChars(rsResult.Fields("Number").Value)


            ……………
……………
Set oElement = xmlDOMDocument.createElement("SIGNATURE") oRoot.appendChild oElement oElement.dataType = "bin.base64" oElement.nodeTypedValue = rsResult.Fields("Signature").Value Else ……………
……………

This ASP file expects an Account Number as a parameter and stores its value in sAccountNumber variable. The ASP File runs the SQL statement to get the account record details. We then create MSXML.DOMDocument object and add BANKACCOUNT as root node. Each field in the table is then added as a child node to BANKACCOUNT root node. The important code here is:

            oElement.dataType = "bin.base64"
      oElement.nodeTypedValue = rsResult.Fields("Signature").Value

When we specify NodeElement.dataType as bin.bae64, the msxml parser internally converts binary data (assigned to NodeElement.nodeTypedValue) to base64.

Save this ASP file in a IIS virtual directory and run it from the browser as http://virdirname/getAcctDetails.asp?AcctNo=1234. You should see:


Figure 2: XML contents returned from the ASP File, as viewed in IE. Note the contents of SIGNATURE tag.

Now, click on View | Source in the Internet Explorer browser and save the source text as .XML file (c:\test.xml). Note the contents of the SIGNATURE tag, it is the base64-encoded form of binary image stored in the database.

So, we saw how msxml parser converts binary to base64. Let's use it again to do the conversion the other way - base64 to binary conversion.

We have a small Visual Basic Application with only two edit boxes, where we specify the names of input XML file and output .GIF file. The input XML file should have the above XML structure. The Visual Basic application loads the XML file, and then reads the content of BANKACCOUNT\SIGNATURE node, and saves it as a binary image .GIF file. Again, we use msxml parser to accomplish this.

Here is what the Visual Basic application looks like:


Figure 3: Visual Basic Application, reads SIGNATURE tag data and saves as binary GIF file, uses msxml parser.

The following code gets the encoded data from the XML and stores it into a gif file:

Private Sub btnOK_Click()
    Dim m_Doc As New MSXML.DOMDocument
    Dim oNode As MSXML.IXMLDOMNode
    
    m_Doc.Load txtInputXMLFile.Text
    
    Set oNode = m_Doc.selectSingleNode("BANKACCOUNT/SIGNATURE")
    
    Dim btArr() As Byte
    btArr = oNode.nodeTypedValue
        
    Open txtOutputImageFile.Text For Binary As #1
    Put #1, 1, btArr
    Close #1
    
    Set m_Doc = Nothing
    Set oNode = Nothing
    
    MsgBox "Signature saved into the gif file with the name " & txtOutputImageFile.Text
    
    End

End Sub

To run this application: open the Visual Basic project, click on Project | References, and check "Microsoft XML, version 2".

Above code loads the XML file; selects BANKACCOUNT/SIGNATURE node and gets the binary data using oNode.nodeTypedValue. This is the place where msxml parser automatically converts base64 data to binary data. Try to open the output .GIF file and you should see my signature.

In this article we saw how we to use the msxml parser for base64 encoding/decoding to transfer binary data as part of XML document.

If you have any questions/comments, feel free to send mail to darshan@PerfectXML.com.

For related articles see:
http://support.microsoft.com/support/kb/articles/Q191/2/39.ASP

http://www.xml.com/pub/98/07/binary/binary.html

http://support.microsoft.com/support/kb/articles/Q254/3/88.ASP

 

Page 2 of 2

 

Previous Page 
 

Recent Jobs

Integration Specialist Needed - Wor
Virtualization Server Infrastructur
A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En

View all Jobs (Add yours)
View all CV (Add yours)




Chicago Web Site Design
teleconferencing
online fax
Burberry sunglasses
air freshener
odor remover


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP