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.
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 & < > " and '. 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.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
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:
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.
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!!