DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: binary data in XML

  1. #1
    Jamie Harsevoort Guest

    binary data in XML

    Hi,

    I've been struggling with storing binary data in an XML document.

    Below is some sample VB code that loads a bitmap into a string buffer, then
    copies the string buffer to XML. Strangely, the XML element cuts off the
    string buffer whereas the VB string data type manages to hold the entire
    bitmap.

    AFAIK, both the VB string datatype and XML store strings in unicode, so I
    cannot come up with a reason for the differences.

    I would greatly appreciate if someone could help me out with this.

    Here is the code (you will need a reference to the MSXML object):

    Private Sub Command1_Click()
    Dim doc As DOMDocument
    Dim testelement As IXMLDOMElement
    Dim ImgBuff As String
    Dim nFile As Integer
    Const conImagePath As String = "c:\winnt\gone fishing.bmp"

    'read bitmap into string buffer
    nFile = FreeFile
    Open conImagePath For Binary Access Read As #nFile
    ImgBuff = Space(LOF(nFile))
    Get #nFile, , ImgBuff
    Close #nFile

    'test saving bitmap from string buffer
    nFile = FreeFile
    Open "c:\test.bmp" For Binary Access Write As #nFile
    Put #nFile, , ImgBuff
    Close #nFile

    'create xml document
    Set doc = New DOMDocument
    Set testelement = doc.createElement("image")
    testelement.Text = ImgBuff
    Debug.Print (testelement.Text = ImgBuff)
    doc.appendChild testelement

    'test saving bitmap from xml document
    nFile = FreeFile
    Open "c:\test2.bmp" For Binary Access Write As #nFile
    Put #nFile, , testelement.Text
    Close #nFile
    End Sub




  2. #2
    Vishwanathan Raman Guest

    Re: binary data in XML


    well i have no solution but i am facing a same problem so if yuo get to know
    some thing please let me know. Just some time before you posted this problem
    i also posted one with the sub "Images in XML"

    Vishy



    "Jamie Harsevoort" <jharsevoort@coyotecorp.com> wrote:
    >Hi,
    >
    >I've been struggling with storing binary data in an XML document.
    >
    >Below is some sample VB code that loads a bitmap into a string buffer, then
    >copies the string buffer to XML. Strangely, the XML element cuts off the
    >string buffer whereas the VB string data type manages to hold the entire
    >bitmap.
    >
    >AFAIK, both the VB string datatype and XML store strings in unicode, so

    I
    >cannot come up with a reason for the differences.
    >
    >I would greatly appreciate if someone could help me out with this.
    >
    >Here is the code (you will need a reference to the MSXML object):
    >
    >Private Sub Command1_Click()
    > Dim doc As DOMDocument
    > Dim testelement As IXMLDOMElement
    > Dim ImgBuff As String
    > Dim nFile As Integer
    > Const conImagePath As String = "c:\winnt\gone fishing.bmp"
    >
    > 'read bitmap into string buffer
    > nFile = FreeFile
    > Open conImagePath For Binary Access Read As #nFile
    > ImgBuff = Space(LOF(nFile))
    > Get #nFile, , ImgBuff
    > Close #nFile
    >
    > 'test saving bitmap from string buffer
    > nFile = FreeFile
    > Open "c:\test.bmp" For Binary Access Write As #nFile
    > Put #nFile, , ImgBuff
    > Close #nFile
    >
    > 'create xml document
    > Set doc = New DOMDocument
    > Set testelement = doc.createElement("image")
    > testelement.Text = ImgBuff
    > Debug.Print (testelement.Text = ImgBuff)
    > doc.appendChild testelement
    >
    > 'test saving bitmap from xml document
    > nFile = FreeFile
    > Open "c:\test2.bmp" For Binary Access Write As #nFile
    > Put #nFile, , testelement.Text
    > Close #nFile
    >End Sub
    >
    >
    >



  3. #3
    Greg Longtin Guest

    Re: binary data in XML

    Jamie,

    See my response to "Images in XML". You need to use byte arrays, and
    declare the image element datatype as "bin.base64".

    Greg Longtin


  4. #4
    L Kvitek Guest

    Re: binary data in XML


    I have been successfully sending WAV files in XML from
    client to server. The WAV file is read into an array of bytes
    and converted to base64 using a control from www.dev-soft.com:

    Private Function BufToEncodedStr(bBuffer() As Byte) As String
    Const PROC_NAME As String = "BufToEncodedStr"
    Dim oEncoder As New NETCODELib.NetCode

    Err.Clear
    On Error GoTo ErrorHandler
    Err.Source = App.ProductName & "." & PROC_NAME

    oEncoder.DecodedDataB = bBuffer()
    oEncoder.Format = f_BASE64
    oEncoder.Action = a_EncodeToString
    BufToEncodedStr = oEncoder.EncodedData

    Exit Function
    ErrorHandler:
    Err.Raise Err.Number, Err.Source, Err.Description

    End Function

    Then simply assign this string to xmldoc.node.text
    and you're ready. Of course on the receiving side
    I decode it and write it back into an array of byte.

    With frmHidden.Netcode1
    .Format = f_BASE64
    .EncodedData = sEncodedStr
    .Action = a_DecodeToString
    nAryMax = UBound(.DecodedDataB)
    nAryMin = LBound(.DecodedDataB)
    vBuf = .DecodedDataB
    ReDim bBuffer(nAryMin To nAryMax)
    For i = nAryMin To nAryMax
    bBuffer(i) = vBuf(i)
    Next
    End With

    "Jamie Harsevoort" <jharsevoort@coyotecorp.com> wrote:
    >Hi,
    >
    >I've been struggling with storing binary data in an XML document.
    >
    >Below is some sample VB code that loads a bitmap into a string buffer, then
    >copies the string buffer to XML. Strangely, the XML element cuts off the
    >string buffer whereas the VB string data type manages to hold the entire
    >bitmap.
    >
    >AFAIK, both the VB string datatype and XML store strings in unicode, so

    I
    >cannot come up with a reason for the differences.
    >
    >I would greatly appreciate if someone could help me out with this.
    >
    >Here is the code (you will need a reference to the MSXML object):
    >
    >Private Sub Command1_Click()
    > Dim doc As DOMDocument
    > Dim testelement As IXMLDOMElement
    > Dim ImgBuff As String
    > Dim nFile As Integer
    > Const conImagePath As String = "c:\winnt\gone fishing.bmp"
    >
    > 'read bitmap into string buffer
    > nFile = FreeFile
    > Open conImagePath For Binary Access Read As #nFile
    > ImgBuff = Space(LOF(nFile))
    > Get #nFile, , ImgBuff
    > Close #nFile
    >
    > 'test saving bitmap from string buffer
    > nFile = FreeFile
    > Open "c:\test.bmp" For Binary Access Write As #nFile
    > Put #nFile, , ImgBuff
    > Close #nFile
    >
    > 'create xml document
    > Set doc = New DOMDocument
    > Set testelement = doc.createElement("image")
    > testelement.Text = ImgBuff
    > Debug.Print (testelement.Text = ImgBuff)
    > doc.appendChild testelement
    >
    > 'test saving bitmap from xml document
    > nFile = FreeFile
    > Open "c:\test2.bmp" For Binary Access Write As #nFile
    > Put #nFile, , testelement.Text
    > Close #nFile
    >End Sub
    >
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center