Save binary data into XML file
Hi,
I have a binary data file that I want to store into a XML file. When I open the XML file it have to create the file inserting the binary data into it again.
Is it possible? I've seen that is possible to store images like qresource does.
Thanks,
Re: Save binary data into XML file
Base64 is the typical standard for storing binary data in XML.
Re: Save binary data into XML file
Hi,
Sorry, but I didn't explained well.
I write an XML file that contains some data. On the other hand I have a file data that I want to store into my XML file. The binary file is from a 3rd party lib that stores some data in its way.
So I want to read the binary data content and write into the XML file in a tag like "<file_data>", then remove the binary file from disk. When I need to open the file I want to read the data of "<file_data>" tag and write it to a file to let the 3rd party open it.
It's just to get only one file.
Thanks,
Re: Save binary data into XML file
Yes, I'm going to assume you know how to read/write/delete disk files, so for the XML part, Qt has XML classes. You can read the binary file into memory, encode as Base64 (using QByteArray::toBase64) and then place the data into a CDATA section of the XML using QDomDocument::createCDATASection, QXmlStreamWriter::writeCDATA, or even just QXmlStreamWriter::writeCharacters.
Re: Save binary data into XML file
Quote:
Originally Posted by
squidge
You can read the binary file into memory, encode as Base64 (using QByteArray::toBase64) and then place the data into a CDATA section of the XML using QDomDocument::createCDATASection, QXmlStreamWriter::writeCDATA, or even just QXmlStreamWriter::writeCharacters.
I have similar issue.
I have a pointer to a buffer contains Binary-data .. I want to pull the values inside the buffer and encoding them before putting them in my xml format .. so I though of this:
QByteArray text;
for (int i=0; i<len_of_my_buffer; i++){
text[i] = *pointer_to_buffer[i];
}
create_xml_function(text.toBase64, "encoded data")
is this right ?
Re: Save binary data into XML file
No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:
Code:
create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!
Of course, you will want to add some error checking...
Re: Save binary data into XML file
Quote:
Originally Posted by
d_stranz
No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:
Code:
create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!
Of course, you will want to add some error checking...
Thanks, it works now and I am passing it to my xml. you were right, my Qbytearray was "empty with no size"