• 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

Can a database stored byte data file attachment be exported and imported through XML with other data

 
Ranch Hand
Posts: 61
2
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem:
I'm receiving an error ("[Fatal Error] three_copies.xml:12:562: Character reference "&#") when I attempt to read an XML file that contains file attachment byte data converted to a string in the XML. The error appears to be triggered a the document parse call:



What I'm trying to do:
Create an import/export option for the end-user to share database notes.

Each database note contains multiple table and field references, one of which is a table called "FILES" with an ID, FileName, and FileData fields with the latter containing byte data. When users view a note the file byte data is retrieved and opened, which could be a jpg, docx, xlsx, etc. If a user exports or imports a note, it may contain one or more file attachments.

What I have done:
I'm able to create an XML file successfully, but I maybe be handling the byte conversion to string wrong. The code below would export the database byte attachment data to a string into the XML:



I was planning to do the following to read and convert the string of bytes then written into a different database:



What I'm expecting:
I'm in the testing phase. During testing and writing to the Console, since the byte data was converted to a string, I wasn't expecting to receive the 'fatal error' I mentioned above with the "&#" characters. I was expecting to see a long string representing the data to be written to the console. I'm not sure exactly what this is telling me? Am I handling the exporting of the byte data wrong? Or maybe I can't have file byte data in an XML with other string data? Not sure exactly.

Lastly, I'm working with an embedded SQLite database in the application if that makes a difference.
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML has 5 "magic" characters. Before embedding text in XML, you have to deal with them.

To do that, you must replace them with what are known as XML "entities" in cases where otherwise the XML would be ambiguous.

The magic characters are & < > " and ' and their entity replacements are &amp; &lt; &gt; &quot; and &apos;. NOTE that the terminating ";" (semicolon) is mandatory.

Here's an example of why you need to substitute;



The first faulty element has a "<" in it, so it looks like you're trying to start a sub-element. But you're not, so the parser rejects it. The second faulty element tries to embed double quotes within double quotes, but that particular escape mechanism may work in CSV files, but not for XML.

So instead:

 
Scott Vallery
Ranch Hand
Posts: 61
2
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Tim!

Thanks for info and I was able to resolve this with your help. I did the following:

Many sites recommended using StringEscapeUtils by Apache Commons (org.apache.commons.text.StringEscapeUtils). It requires the following two downloads (latest stable versions), Apache Commons "text" and "lang" jars. There is a dependency between the two, so you need both:

commons-text-1.6.jar     (Download: http://commons.apache.org/proper/commons-text/download_text.cgi)
commons-lang3-3.9.jar   (Download: http://commons.apache.org/proper/commons-lang/download_lang.cgi)

I then imported and applied the StringEscapeUtils.escapeXml10 to my file data string at the time of the XML document creation. I then used StringEscapeUtils.unescapeXML during the reading of the XML document to correctly present the data.

At the time of XML document element creation:

At the time of reading the XML document:


Thanks for the help!
 
Ranch Hand
Posts: 729
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're 100% certain that it is a text file kind of data, you can do it like this (and I suppose you've caught whatever reported exceptions needed already).


If eventually those are files of binary data like images or office spreedsheet or whatever, you can Base64 encoding the data to embed it in the text nodes... In java8+, you've the support natively.

In jdk1.6 or 1.7, you can still look for helpers like javax.xml.bind.DateTypeConverter, or org.apache.commons.codec.Base64 or else you can find.
 
Tim Holloway
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, forgot about CDATA - the all-purpose wrapper for non-XML-friendly content!
 
Scott Vallery
Ranch Hand
Posts: 61
2
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g tsuji and Tim!

Cool thanks for the input! I ended up using the Base64 wrapper and it worked great as well. I was running into an issue with byte[] > XML String > byte[] and Base64 seemed to solve my issue after this solution.

I'm good with this being resolved if you concur! Thanks!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!