<p>I have two applications written in Java that communicate with each other using XML messages over the network.  I'm using a SAX parser at the receiving end to get the data back out of the messages.  One of the requirements is to embed binary data in an XML message, but SAX doesn't like this.  Does anyone know how to do this?</p>

<p>UPDATE:  I got this working with the <a href="http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html" rel="noreferrer">Base64</a> class from the <a href="http://commons.apache.org/codec/" rel="noreferrer">apache commons codec library</a>, in case anyone else is trying something similar.</p>

[{'answer_id': 19898, 'author': 'mercutio', 'author_id': 1951, 'author_profile': 'https://Stackoverflow.com/users/1951', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe encode them into a known set - something like base 64 is a popular choice.</p>\n'}, {'answer_id': 19903, 'author': 'basszero', 'author_id': 287, 'author_profile': 'https://Stackoverflow.com/users/287', 'pm_score': 3, 'selected': False, 'text': '<p>Try Base64 encoding/decoding your binary data. Also look into CDATA sections</p>\n'}, {'answer_id': 19904, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 9, 'selected': True, 'text': '<p>You could encode the binary data using base64 and put it into a Base64 element; the below article is a pretty good one on the subject.</p>\n\n<p><a href="http://www.xml.com/pub/a/98/07/binary/binary.html" rel="noreferrer">Handling Binary Data in XML Documents</a></p>\n'}, {'answer_id': 19909, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 3, 'selected': False, 'text': '<p>I usually encode the binary data with <a href="http://en.wikipedia.org/wiki/Base64" rel="nofollow noreferrer">MIME Base64</a> or <a href="https://www.rfc-editor.org/rfc/rfc3986" rel="nofollow noreferrer">URL encoding</a>.</p>\n'}, {'answer_id': 19939, 'author': 'Mo.', 'author_id': 1870, 'author_profile': 'https://Stackoverflow.com/users/1870', 'pm_score': 8, 'selected': False, 'text': '<p>XML is so versatile...</p>\n\n<pre><code>&lt;DATA&gt;\n  &lt;BINARY&gt;\n    &lt;BIT index="0"&gt;0&lt;/BIT&gt;\n    &lt;BIT index="1"&gt;0&lt;/BIT&gt;\n    &lt;BIT index="2"&gt;1&lt;/BIT&gt;\n    ...\n    &lt;BIT index="n"&gt;1&lt;/BIT&gt;\n  &lt;/BINARY&gt;\n&lt;/DATA&gt;\n</code></pre>\n\n<p>XML is like violence - If it doesn\'t solve your problem, you\'re not using enough of it.</p>\n\n<p>EDIT:</p>\n\n<p>BTW: Base64 + CDATA is probably the best solution</p>\n\n<p>(EDIT2:<br>\nWhoever upmods me, please also upmod the real answer. We don\'t want any poor soul to come here and actually implement my method because it was the highest ranked on SO, right?)</p>\n'}, {'answer_id': 20646, 'author': 'Boris Terzic', 'author_id': 1996, 'author_profile': 'https://Stackoverflow.com/users/1996', 'pm_score': 5, 'selected': False, 'text': '<p>Base64 is indeed the right answer but CDATA is not, that\'s basically saying: "this could be anything", however it must <strong>not</strong> be just anything, it has to be Base64 encoded binary data. XML Schema defines <a href="http://www.w3.org/TR/xmlschema-2/#base64Binary" rel="noreferrer">Base 64 binary as a primitive datatype</a> which you can use in your xsd.</p>\n'}, {'answer_id': 36955, 'author': 'Andrei Savu', 'author_id': 3885, 'author_profile': 'https://Stackoverflow.com/users/3885', 'pm_score': 2, 'selected': False, 'text': '<p>You can also <a href="http://en.wikipedia.org/wiki/Uuencode" rel="nofollow noreferrer">Uuencode</a> you original binary data. This format is a bit older but it does the same thing as base63 encoding. </p>\n'}, {'answer_id': 3156135, 'author': 'Jarek Przygódzki', 'author_id': 293933, 'author_profile': 'https://Stackoverflow.com/users/293933', 'pm_score': 3, 'selected': False, 'text': '<p>Any <a href="http://en.wikipedia.org/wiki/Binary-to-text_encoding" rel="noreferrer">binary-to-text encoding</a> will do the trick. I use something like that</p>\n\n<pre><code>&lt;data encoding="yEnc&gt;\n&lt;![CDATA[ encoded binary data ]]&gt;\n&lt;/data&gt;\n</code></pre>\n'}, {'answer_id': 3400927, 'author': 'Baxter Tidwell', 'author_id': 2055692, 'author_profile': 'https://Stackoverflow.com/users/2055692', 'pm_score': 4, 'selected': False, 'text': '<p>I had this problem just last week. I had to serialize a PDF file and send it, inside an XML file, to a server.</p>\n\n<p>If you\'re using .NET, you can convert a binary file directly to a base64 string and stick it inside an XML element.</p>\n\n<pre><code>string base64 = Convert.ToBase64String(File.ReadAllBytes(fileName));\n</code></pre>\n\n<p>Or, there is a method built right into the XmlWriter object. In my particular case, I had to include Microsoft\'s datatype namespace:</p>\n\n<pre><code>StringBuilder sb = new StringBuilder();\nSystem.Xml.XmlWriter xw = XmlWriter.Create(sb);\nxw.WriteStartElement("doc");\nxw.WriteStartElement("serialized_binary");\nxw.WriteAttributeString("types", "dt", "urn:schemas-microsoft-com:datatypes", "bin.base64");\nbyte[] b = File.ReadAllBytes(fileName);\nxw.WriteBase64(b, 0, b.Length);\nxw.WriteEndElement();\nxw.WriteEndElement();\nstring abc = sb.ToString();\n</code></pre>\n\n<p>The string abc looks something that looks like this:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;\n&lt;doc&gt;\n    &lt;serialized_binary types:dt="bin.base64" xmlns:types="urn:schemas-microsoft-com:datatypes"&gt;\n        JVBERi0xLjMKJaqrrK0KNCAwIG9iago8PCAvVHlwZSAvSW5mbw...(plus lots more)\n    &lt;/serialized_binary&gt;\n&lt;/doc&gt;\n</code></pre>\n'}, {'answer_id': 9326197, 'author': 'Jamie', 'author_id': 1215919, 'author_profile': 'https://Stackoverflow.com/users/1215919', 'pm_score': 2, 'selected': False, 'text': '<p>While the other answers are mostly fine, you could try another, more space-efficient, encoding method like yEnc. (<a href="https://en.wikipedia.org/wiki/YEnc" rel="nofollow noreferrer">yEnc wikipedia link</a>)  With yEnc also get checksum capability right "out of the box". Read and links below. Of course, because XML does not have a native yEnc type your XML schema should be updated to properly describe the encoded node.</p>\n\n<p><strong>Why</strong>: Due to the encoding strategies base64/63, uuencode et al. encodings increase the amount of data (overhead) you need to store and transfer by roughly 40% (vs. yEnc\'s 1-2%). Depending on what you\'re encoding, 40% overhead could be/become an issue. </p>\n\n<hr>\n\n<p><strong>yEnc - Wikipedia abstract:</strong>\n<a href="https://en.wikipedia.org/wiki/YEnc" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/YEnc</a>\nyEnc is a binary-to-text encoding scheme for transferring binary files in messages on Usenet or via e-mail. ... An additional advantage of yEnc over previous encoding methods, such as uuencode and Base64, is the inclusion of a CRC checksum to verify that the decoded file has been delivered intact.\n\u200e</p>\n'}, {'answer_id': 17478845, 'author': 'KrisWebDev', 'author_id': 2227298, 'author_profile': 'https://Stackoverflow.com/users/2227298', 'pm_score': 2, 'selected': False, 'text': '<p>Base64 overhead is 33%.</p>\n\n<p><a href="https://github.com/kriswebdev/BaseXML/blob/master/README.md" rel="nofollow noreferrer">BaseXML</a> for XML1.0 <strong>overhead is only 20%</strong>. But it\'s not a standard and only have a C implementation yet. Check it out if you\'re concerned with data size. Note that however browsers tends to implement compression so that it is less needed.</p>\n\n<p>I developed it after the discussion in this thread: <a href="https://stackoverflow.com/questions/17301940/encoding-binary-data-within-xml-alternatives-to-base64/17354584#17354584">Encoding binary data within XML : alternatives to base64</a>.</p>\n'}, {'answer_id': 27396540, 'author': 'Lie Ryan', 'author_id': 309412, 'author_profile': 'https://Stackoverflow.com/users/309412', 'pm_score': 0, 'selected': False, 'text': '<p>If you have control over the XML format, you should turn the problem inside out. Rather than attaching the binary XML you should think about how to enclose a document that has multiple parts, one of which contains XML.</p>\n<p>The traditional solution to this is an archive (e.g. tar). But if you want to keep your enclosing document in a text-based format or if you don\'t have access to an file archiving library, there is also a standardized scheme that is used heavily in email and HTTP which is <a href="https://www.rfc-editor.org/rfc/rfc2387" rel="nofollow noreferrer">multipart/* MIME</a> with <a href="https://www.rfc-editor.org/rfc/rfc3030" rel="nofollow noreferrer">Content-Transfer-Encoding: binary</a>.</p>\n<p>For example if your servers communicate through HTTP and you want to send a multipart document, the primary being an XML document which refers to a binary data, the HTTP communication might look something like this:</p>\n<pre><code>POST / HTTP/1.1\nContent-Type: multipart/related; boundary=&quot;qd43hdi34udh34id344&quot;\n... other headers elided ...\n\n--qd43hdi34udh34id344\nContent-Type: application/xml\n\n&lt;myxml&gt;\n    &lt;data href=&quot;cid:data.bin&quot;/&gt;\n&lt;/myxml&gt;\n--qd43hdi34udh34id344\nContent-Id: &lt;data.bin&gt;\nContent-type: application/octet-stream\nContent-Transfer-Encoding: binary\n\n... binary data ...\n--qd43hdi34udh34id344--\n</code></pre>\n<p>As in above example, the XML refer to the binary data in the enclosing multipart by using a <code>cid</code> URI scheme which is an identifier to the Content-Id header. The overhead of this scheme would be just the MIME header. A similar scheme can also be used for HTTP response. Of course in HTTP protocol, you also have the option of sending a multipart document into separate request/response.</p>\n<p>If you want to avoid wrapping your data in a multipart is to use data URI:</p>\n<pre><code>&lt;myxml&gt;\n    &lt;data href=&quot;data:application/something;charset=utf-8;base64,dGVzdGRhdGE=&quot;/&gt;\n&lt;/myxml&gt;\n</code></pre>\n<p>But this has the base64 overhead.</p>\n'}]
['https://Stackoverflow.com/questions/19893', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1288/']