Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

store binary data in xml?

282 views
Skip to first unread message

stan

unread,
Dec 14, 2004, 3:45:03 AM12/14/04
to

How to store binary data in xml?

Thanks.

Nils Haeck

unread,
Dec 14, 2004, 4:14:26 AM12/14/04
to
Hi Stan,

You can use BASE64 encoding for that.

Check out our XmlDocuments component, it supports automatic BASE64 encoding
through the TXmlNode.BinaryString property.
www.simdesign.nl/xml.html

Kind regards,

Nils Haeck
www.simdesign.nl

"stan" <st...@yahoo.com> wrote in message
news:41bea80f$1...@newsgroups.borland.com...

eshipman

unread,
Dec 14, 2004, 9:26:51 AM12/14/04
to
In article <41bea80f$1...@newsgroups.borland.com>, st...@yahoo.com says...

>
> How to store binary data in xml?
>

Base64 encode and enclose in a CDATA section.

This is a modified snippet of code from my XMLTable control
that converts a Dataset to XML, this is the code that handles
ftTypedBinary, ftBlob, and ftGraphic field types.

var
oXMLDoc: TdomDocument;
oRoot: TdomNode;
oChild: TdomElement;
oXMLdecl: TdomXmlDeclaration;
oXMLCDATASection: TdomCDATASection;
DecBlobStream: TMemoryStream;
EncBlobStream: TMemoryStream;
cDatasl: TStringList;
sFieldName: String;
sFieldValue: String;
begin
oXMLDoc := TdomDocument.Create;
try
oRoot := nil;
oChild := nil;
oNode := nil;
oAttr := nil;
oXMLdecl := nil;
oXMLCDATASection := nil;
oDTD := nil;
oRoot := TdomNode.Create(oXMLDoc);
oRoot := FXMLDoc.CreateElement('ROWSET');
oXMLDoc.AppendChild(oRoot);
oXMLdecl := oXMLDoc.CreateXmlDeclaration('1.0', 'UTF-8', 'yes');
oXMLDoc.InsertBefore(oXMLdecl, oXMLDoc.ChildNodes.Item(0));
oNode := oXMLDoc.CreateElement('ROW');
oXMLDoc.DocumentElement.AppendChild(oNode);
// Can't have spaces in Node Name so replace with underscore '_'
sFieldName := StringReplace(Source.FieldList.Fields[i].FieldName,
' ', '_', [rfReplaceAll]);
oChild := FXMLDoc.CreateElement(sFieldName);
oNode.AppendChild(oChild);
cDatasl := TStringList.Create;
DecBlobStream := TMemoryStream.Create;
EncBlobStream := TMemoryStream.Create;
try
TBlobField(Source.Fields[i]).SaveToStream(DecBlobStream);
DecBlobStream.Position := 0;
// base64 encoding source cut from Andreas
// Horstmeier's TCP/IP component suite
// email me at mr_delphi_developer at yahoo dot com
// if you want the converted source
cDatasl := encode_base64(DecBlobStream);
sFieldValue := cDatasl.Text;
oXMLCDATASection := TdomCDATASection.Create(oXMLDoc);
oXMLCDATASection.Data := sFieldValue;
oChild.AppendChild(oXMLCDATASection);
finally
FreeAndNil(EncBlobStream);
FreeAndNil(DecBlobStream);
FreeAndNil(cDatasl);
end;
finally
FreeAndNil(oXMLDoc);
end;
end;

Victor Hadianto

unread,
Dec 19, 2004, 7:04:13 PM12/19/04
to
>> How to store binary data in xml?
>>
>
> Base64 encode and enclose in a CDATA section.

Do you need to store it in CDATA section? Once it's Base64 encoded I think
you should be able to store it as an element.

eshipman

unread,
Dec 20, 2004, 9:00:35 AM12/20/04
to
In article <41c61682$1...@newsgroups.borland.com>, vic...@synop.com says...

If you really want to know, check the RFC. I think it
is a requirement.

danny heijl

unread,
Dec 20, 2004, 2:22:17 PM12/20/04
to
eshipman schreef:

> If you really want to know, check the RFC. I think it
> is a requirement.

base64Binary is a native XML data type (in XML schema).

Danny
---

eshipman

unread,
Dec 20, 2004, 2:48:38 PM12/20/04
to
In article <41c7266c$1...@newsgroups.borland.com>, danny heijl
<danny_dot_heijl_at_telenet_dot_be> says...

But he didn't specify that he would be using a schema and thus I gave
him an answer that he could use without using one or with using a DTD
only.

0 new messages