Wednesday, 15 August 2007

Storing binary information in XML

Sometimes we need to store some binary information within XML format, which is textual.
Designing schemata for your data you should avoid using binary representations if possible, but there are some cases when you absolutely need it. For example, what if you want to store a picture within your XML?

The problem is that binary data may contain special characters which are prohibited for use in XML.

The solution to this problem is to recode your binary data into a textual form using, for example, Base64 or UUEncode algorithms. The following fragment of Java code demonstrates how to convert an array of bytes (a chunk of binary data) into a properly formed String using Base64 algorithm.

byte[] data = getMyBinaryData();
String encodedData = new sun.misc.BASE64Encoder().encode(data);

You then can use encodedData as a value for some XML element.

The reverse process of decoding a String into an array of bytes can be done using the following Java code:

String encodedData = getEncodedData();
byte[] data = new sun.misc.BASE64Decoder().decodeBuffer(encodedData);

UUEncode/UUDecode algorithms are also available in sun.misc package.
This approach will, unfortunately, fail in an Applet, as the access to sun. packages is prohibited for applets. You have to use a different implementation of a transcoding algorithm in such cases. One of the alternatives is:

No comments: