Base64 Encoding in XML
Base64 encoding offers a way to represent binary data using ASCII character strings, thus allowing different types of computers to exchange binary information. Once converted, or encoded, you can then place the ASCII string in an XML document. Encoded strings also can be used to shorten URLs or to mangle text as a simple security measure.
-
Encode Your Binary Data
-
Many programming languages support the encoding of binary data, either through programming interfaces or directly through the language. Hence software programmers typically encode their binary data programmatically. You also may use a utility program to encode a binary file and create your encoded ASCII string.
Place the Encoding Into Your XML Document
-
It is possible to store string data within an XML element. However, the preferred method for large strings is to pass your encoded data within a CDATA section of the XML document. Because XML does not parse the data in CDATA sections, the operation uses less memory and can speed the overall process. CDATA sections also may contain programming instructions for decoding the ASCII string on the receiving end.
-
Decode the ASCII String
-
A program on the receiving end typically will access the XML document, extract the ASCII string from the CDATA section and reconstruct the original binary data. Another simple method is to place the ASCII string into a text file and feed the file to a decoding utility.
Alternatives to Base64 Encoding
-
In 2005 the World Wide Web Consortium put forth the "XML-binary Optimized Packaging" specification, which directly supports the transmission of binary information within XML. Unless your XML parser does not support this feature or your application requires that your binary objects be encoded, consider using this extension to the XML specification.
-