|
Home > Products > XML Tools - Stylus Studio Features > Import Export XML > Binary XML Import Export XML
Binary XML
Ah, the age-old problem (well, at least since XML was invented) is how to embed binary data in XML? Never mind why; metaphysics is outside the scope of this document. We're going to tell you how to put it in, and how to get it out. XML is not the idea carrier for binary data. It is a text format, and as such doesn't cope well with raw bits. But if binary data is properly encoded, using something like the W3C XML Schema types base64Binary or hexBinary, then using the XML Converters reading and writing binary files from XSLT and/or XQuery becomes a snap. base64Binary, Base-64 and XMLWe're going to use the Base-64 encoding format for this demonstration, since it packs tighter than hex encoding. But jump to the end to see hexBinary covered quickly. Binary to XML
First, let's encode binary as XML. We'll take the shirt images that we used in the XML Report demonstration. Our source material will then be:
The XML file will be very simple:
And the XSLT not much bigger:
So, how does it work? For each <item>, it wraps it in a new <file> and attaches an attribute named "name" holding the input file name. The content comes from the shirt file and through the Base-64 Deployment Adapter. That adapter takes any raw data and returns it as Base-64 encoded data, which is entirely compatible with XML. (Also note that I've made the reference to the files absolute and pointing to our server, if they were local you could just say <xsl:value-of select="document(concat('adapter:Base-64?', .))"/> without all of the pathiness.) This makes the resultant XML document look like this:
That XML to BinaryNow we've got it in; how do we get it out? Supplying that same document we just created as input to this next XSLT document will give us the five shirts' worth of GIF files again. (And we've added a little bit to write out status messages while it is working so we know what we've created.)
The key here is the hexBinary, Hex and XMLExactly the same results can be achieved with hex-encoded data. Just use the Binary Deployment Adapter, which by default uses base-16 for encoding — just what the W3C XML Schema data type hexBinary expects. Read and Write Binary XMLThere isn't really any such thing as "binary XML", but even that won't stop you from mixing binary and text inside XML thanks to the Base-64 and Binary Converters. The target format doesn't even have to be a file; the adapter could be used in a web serving environment to feed images right from source XML. Since you control the code, there are no limits. Examine the adapters and the rest of Stylus Studio® 2008 XML Enterprise Suite, Release 2 by downloading an evaluation copy and trying these samples today!
Stylus Studio XQuery Innovator AwardsThe Stylus Studio® First Annual XQuery Innovator Awards offers enterprise software developers, systems integrators, and XML professionals the opportunity to have their work publicly showcased and gain valuable recognition as XQuery pioneers. Flat File to XML MapperConvert any flat file to XML in 3 clicks or less using the Stylus Studio Flat File to XML Mapper. Supports tons of different file encodings: UTF-8, UTF-16, US-ASCII, ISO-8859-1, and more. EDIFACT ReferenceThe EDIFACT Reference is a free developer resource providing the most in-depth information on EDIFACT constructs and data models. Introduction to Convert to XMLConvert to XML is Stylus Studio's all-purpose utility for getting legacy data integration of all sorts into a custom XML format. This video provides an overview of the tool and walks you through a sample CSV to XML data transformation project. Stylus Most Wanted |
How do I get my binary data into XML?