Results 1 to 4 of 4

Thread: Reading XML file into a data file correctly?

  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading XML file into a data file correctly?

    I want to save my application data into a project file, storing data of all the open sub-windows, and the used XML file (QDomDocument) in memory. At first, I save the widget data of sub-windows, then I save the XML into a temporary file (because it must be written with QTextStream, but sub-window data is saved with QDataStream, as project file must be written that way), then I open this temporary file again and pass a QDataStream to it, so I can read all the content of it into the project file.

    The problem is that somehow when I read all the bytes from temporary XML file, some additional garbage bytes are also added into the project file during writing it out to the stream. The temporary source XML file is correct, and the read bytes into QByteArray variable also shows them correct, except the result in the project file.

    What causes this? How should I correctly merge these files? If you have better idea on better algorithm, then it is also fine to me.

    Here is the test code:

    Qt Code:
    1. // Here I create the temporary XML file to store XML data from memory to disk
    2. fileXML.setAutoRemove(false);
    3. if (!fileXML.open())
    4. {
    5. log(QString("Cannot read file %1: %2.").arg(fileXML.fileName()).arg(fileXML.errorString()), 1);
    6. file.close();
    7. return;
    8. }
    9. QTextStream outXML(&fileXML);
    10. mergedXML.save(outXML, 0);
    11. fileXML.flush();
    12. fileXML.close();
    13.  
    14. // Here, I open this file again for data stream reading
    15.  
    16. if (!fileXML.open())
    17. {
    18. log(QString("Cannot read file %1: %2.").arg(fileXML.fileName()).arg(fileXML.errorString()), 1);
    19. file.close();
    20. return;
    21. }
    22. x = fileXML.readAll();
    23. out << x; // This is the stream for project file storage
    24.  
    25. //This file is created just to test the behavior when I only save XML data.... result is the same :(
    26. QFile testfile("C:/test.txt");
    27. testfile.open(QIODevice::WriteOnly);
    28. QDataStream teststream(&testfile);
    29. teststream << x;
    30. testfile.flush();
    31. testfile.close();
    To copy to clipboard, switch view to plain text mode 

    Here is the content of test.txt after saving XML data from memory:

    00 00 44 A9 3C 4D 65 72 67 65 64 4D 65 61 73 75 72 65 6D 65 6E 74 73 3E

    which is:

    00 00 44 A9<MergedMeasurements>

    So, there is an extra 4 bytes in front of <Merg... XML data, however variable 'x' which I read the bytes into is correct.

    Why those extra 4 bytes are written into the file? What causes this?

    Thanks!


    Added after 54 minutes:


    I thought that I could overcome this problem temporary while I'm waiting for a useful input by including the following line in the code:

    projectFile.seek(projectFile.pos() + 4); // so, this way i can skip the garbage bytes...

    It is working on small XML data, but if the file is bigger than 65535 Bytes, it fails too due the same garbage 00 00 xy wz Bytes. Why these double zero and two unknown Bytes are written into the file? 2^16 is a round number, so I believe it could be a normal behavior of writing QByteArray into files. ........ Hmm. I give a try of setting XML doc with ByteArray...


    Added after 15 minutes:


    It didn't work.
    This is how I set the version of the stream.... maybe...

    out.setVersion(QDataStream::Qt_4_7);
    Last edited by falconium; 9th May 2011 at 04:34.

  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: Reading XML file into a data file correctly?

    Have you tried testfile.write() instead of testfile << x ?

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

    falconium (9th May 2011)

  4. #3
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading XML file into a data file correctly?

    Thanks!!! It works!
    Why does QDataStream write these crappy data into the file?

  5. #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: Reading XML file into a data file correctly?

    because QDataStream (well, QByteArray) writes the length before the data so that you can use >> to read the data back out of the file without having to search for a delimiter.

Similar Threads

  1. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 2nd May 2011, 23:47
  2. Replies: 1
    Last Post: 27th January 2011, 09:19
  3. Reading data from xls file
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2010, 09:33
  4. reading data from file
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2010, 10:31
  5. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24

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.