PDA

View Full Version : Saving as XML



rakkar
30th October 2009, 15:32
Hi,

Currently, I allow loading and saving of my application using code as follows:



QDataStream dataStream;
dataStream.setDevice(&file);
QByteArray geo_data = saveGeometry();
QByteArray layout_data = saveState();
dataStream << geo_data;
dataStream << layout_data;
dataStream << databases;
dataStream << mostRecentProjectPath;
dataStream << mostRecentDatabasePath;
dataStream << mostRecentLayoutPath;
...


The problem with this is that for future versions, the format of the save file may change. I may need more fields, or fields may no longer apply, or they may be in a different order.

Since I'm probably not the first person to encounter this, I wanted to see if other people had suggestions on the best approach to handle this. Right now I'm thinking if QT has a way to save using something like QXMLDataSTream that would be perfect. With XML, if a field doesn't exist already I could use a default, and I can ignore fields that no longer apply.

I also like the idea of XML because it can be version controlled and modified by hand.

Thanks in advance for any advice.

Lykurg
30th October 2009, 17:15
Did you see QSettings? Or set a magic number (= version number) at the beginning of the stream and then read the informations differently.

vcp
30th October 2009, 17:17
Hi rakkar,

To you save your config file or other things in xml format, you can use QXmlStreamWriter. Read about this class and QtXml Module


Example:


QFile f(_fileManifest);
if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
return(FALSE);

QString manifestNS = QString::fromLatin1("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0");

QXmlStreamWriter stream(&f);
stream.setAutoFormatting(true);

stream.writeNamespace(manifestNS, QString::fromLatin1("manifest"));

stream.writeStartDocument();
stream.writeStartElement(manifestNS, QString::fromLatin1("manifest"));

stream.writeEmptyElement(manifestNS, QString::fromLatin1("file-entry"));
stream.writeAttribute(manifestNS, QString::fromLatin1("media-type"), QString::fromLatin1("application/vnd.oasis.opendocument.spreadsheet"));
stream.writeAttribute(manifestNS, QString::fromLatin1("version"), QString::fromLatin1("1.2"));
stream.writeAttribute(manifestNS, QString::fromLatin1("full-path"), QString::fromLatin1("/"));
stream.writeEmptyElement(manifestNS, QString::fromLatin1("file-entry"));
stream.writeAttribute(manifestNS, QString::fromLatin1("media-type"), QString::fromLatin1("text/xml"));
stream.writeAttribute(manifestNS, QString::fromLatin1("full-path"), QString::fromLatin1("content.xml"));
stream.writeEndElement(); // end manifest:manifest
stream.writeEndDocument();


The result will be:



<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:version="1.2" manifest:full-path="/"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
</manifest:manifest>