PDA

View Full Version : Reading XML file into a data file correctly?



falconium
9th May 2011, 04:34
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:



// Here I create the temporary XML file to store XML data from memory to disk
QTemporaryFile fileXML;
fileXML.setAutoRemove(false);
if (!fileXML.open())
{
log(QString("Cannot read file %1: %2.").arg(fileXML.fileName()).arg(fileXML.errorString( )), 1);
file.close();
return;
}
QTextStream outXML(&fileXML);
mergedXML.save(outXML, 0);
fileXML.flush();
fileXML.close();

// Here, I open this file again for data stream reading

if (!fileXML.open())
{
log(QString("Cannot read file %1: %2.").arg(fileXML.fileName()).arg(fileXML.errorString( )), 1);
file.close();
return;
}
QByteArray x;
x = fileXML.readAll();
out << x; // This is the stream for project file storage

//This file is created just to test the behavior when I only save XML data.... result is the same :(
QFile testfile("C:/test.txt");
testfile.open(QIODevice::WriteOnly);
QDataStream teststream(&testfile);
teststream << x;
testfile.flush();
testfile.close();

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);

squidge
9th May 2011, 07:42
Have you tried testfile.write() instead of testfile << x ?

falconium
9th May 2011, 16:09
Thanks!!! It works!
Why does QDataStream write these crappy data into the file? :confused:

squidge
9th May 2011, 18:55
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.