Click to See Complete Forum and Search --> : binary data in XML
Jamie Harsevoort
12-13-2000, 03:21 PM
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
Vishwanathan Raman
12-13-2000, 03:56 PM
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
>
>
>
Greg Longtin
12-14-2000, 03:09 PM
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
L Kvitek
12-18-2000, 03:44 PM
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
>
>
>
devx.com
Copyright Internet.com Inc. All Rights Reserved