PDA

View Full Version : Save binary data into XML file



^NyAw^
20th January 2010, 15:31
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,

squidge
20th January 2010, 15:46
Base64 is the typical standard for storing binary data in XML.

^NyAw^
20th January 2010, 17:02
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,

squidge
20th January 2010, 17:57
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.

SudanBoy
21st March 2013, 15:20
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 ?

d_stranz
21st March 2013, 15:31
is this right ?

No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:



QByteArray text64 = QByteArray::fromRawData( pointer_to_buffer, len_of_my_buffer ).toBase64();
create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!


Of course, you will want to add some error checking...

SudanBoy
22nd March 2013, 11:16
No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:



QByteArray text64 = QByteArray::fromRawData( pointer_to_buffer, len_of_my_buffer ).toBase64();
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"