Results 1 to 7 of 7

Thread: Save binary data into XML file

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save binary data into XML file

    Base64 is the typical standard for storing binary data in XML.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  5. The following user says thank you to squidge for this useful post:

    ^NyAw^ (21st January 2010)

  6. #5
    Join Date
    Mar 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save binary data into XML file

    Quote Originally Posted by squidge View Post
    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 ?

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Save binary data into XML file

    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:

    Qt Code:
    1. QByteArray text64 = QByteArray::fromRawData( pointer_to_buffer, len_of_my_buffer ).toBase64();
    2. create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!
    To copy to clipboard, switch view to plain text mode 

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

  8. #7
    Join Date
    Mar 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save binary data into XML file

    Quote Originally Posted by d_stranz View Post
    No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:

    Qt Code:
    1. QByteArray text64 = QByteArray::fromRawData( pointer_to_buffer, len_of_my_buffer ).toBase64();
    2. create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!
    To copy to clipboard, switch view to plain text mode 

    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"

Similar Threads

  1. Replies: 12
    Last Post: 31st October 2010, 17:08
  2. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  3. can`t read a binary data!
    By blm in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 16:56
  4. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.