I think this is the correct place to post for an XML file conversion stream??
The escaping of file bytes and converting to string using the Apache StringEscapeUtils for XML file transmission appears to be corrupting the file bytes. The StringEscapeUtils is necessary to allow the byte data to be embedded in the XML and prevent parsing issues.
What I have done:
To be sure, I've isolated the issue to this recommended StringEscapeUtils process. The following line of code casts an object array of bytes back to bytes. I have no issue thereafter this code creating and opening any file (e.g jpg, xlsx, docx, png, txt... etc) using this byte array from this line.
If I take these file bytes and escape and unescape them during the import export processing of the XML doc it somehow corrupts my files.
Code escaping the byte data to string and creating the <filedata> node:
Code unescaping the <filedata> tag data:
Any thoughts on what I may be missing? Maybe I need to escape the bytes differently?
Well, I suppose that's a solution if it works for you. But I have a couple of comments.
First of all, XML is a text format. So when I see you're trying to put bytes into an XML document, already I see a red flag over that design.
However if it's really arbitrary bytes which you might be putting into the XML, then using Base64 to convert them to text is not a bad idea.
It's just that I get the impression that you were using an Apache utility whose purpose is to escape characters like & which need to be escaped in an XML text node. But if you're using a DOM to build the XML you don't need to do that, it's handled automatically when you serialize the DOM. Or otherwise it's preferable to use a CDATA section, where the contents don't need to be XML-escaped.
So what's the deal? Are these "bytes" actually text?