How do you embed binary data in XML? How do you embed binary data in XML? java java

How do you embed binary data in XML?


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.

Handling Binary Data in XML Documents


XML is so versatile...

<DATA>  <BINARY>    <BIT index="0">0</BIT>    <BIT index="1">0</BIT>    <BIT index="2">1</BIT>    ...    <BIT index="n">1</BIT>  </BINARY></DATA>

XML is like violence - If it doesn't solve your problem, you're not using enough of it.

EDIT:

BTW: Base64 + CDATA is probably the best solution

(EDIT2:
Whoever 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?)


Base64 is indeed the right answer but CDATA is not, that's basically saying: "this could be anything", however it must not be just anything, it has to be Base64 encoded binary data. XML Schema defines Base 64 binary as a primitive datatype which you can use in your xsd.