Sunday, December 09, 2007

Binary Data in XML

I was lately working on one of my projects when i encountered a situation wherein i needed to include binary data within XML. My first reaction, thats fine, one just need to drop some binary data between tags. But then, i realized this could lead to couple of problems. For example
1. Binary data could include null characters
2. Binary data could include </ sequence.

So while googling i came across two solutions.

CDATA

One obvious solution was to use the CDATA tag. The XML parser would ignore anything thats between CDATA and hence the appearence of "</" sequence would be ignored.

But this doesnt seemed a foolproof solution.

Text Based Encryption
So i sticked to the second method which was to encrypt the binary data to text based formatting.

The most popular algorithm that could be used in this scenario is the base64 encoding. So what i did in my application was to encode the binary data in Base6 4 and embedd that data in the XML.