• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How do I prevent the escaping of file bytes to XML string for importing/exporting corrupting files?

 
Ranch Hand
Posts: 61
2
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is the correct place to post for an XML file conversion stream??

My Problem:
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?

Thanks!

 
Scott Vallery
Ranch Hand
Posts: 61
2
IntelliJ IDE Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolved my own issue!

After further research the solution was the use of Base64.  --  Base64.getEncoder().encodeToString(bytes)

Base64 during the XML creation (export) process:


Base64 during the XML reading (import) process to create the file: -- Base64.getDecoder().decode(childList.item(cl).getTextContent().getBytes())

 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now I notice this in your original post:

Scott Vallery wrote:...opening any file (e.g jpg, xlsx, docx, png, txt... etc) ...



So yeah, arbitrary bytes it is. In which case Base64 is the way to go.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!