Results 1 to 3 of 3

Thread: Saving as XML

  1. #1
    Join Date
    Aug 2009
    Posts
    56
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Saving as XML

    Hi,

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

    Qt Code:
    1. QDataStream dataStream;
    2. dataStream.setDevice(&file);
    3. QByteArray geo_data = saveGeometry();
    4. QByteArray layout_data = saveState();
    5. dataStream << geo_data;
    6. dataStream << layout_data;
    7. dataStream << databases;
    8. dataStream << mostRecentProjectPath;
    9. dataStream << mostRecentDatabasePath;
    10. dataStream << mostRecentLayoutPath;
    11. ...
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Saving as XML

    Did you see QSettings? Or set a magic number (= version number) at the beginning of the stream and then read the informations differently.

  3. #3
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving as XML

    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:
    Qt Code:
    1. QFile f(_fileManifest);
    2. if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
    3. return(FALSE);
    4.  
    5. QString manifestNS = QString::fromLatin1("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0");
    6.  
    7. QXmlStreamWriter stream(&f);
    8. stream.setAutoFormatting(true);
    9.  
    10. stream.writeNamespace(manifestNS, QString::fromLatin1("manifest"));
    11.  
    12. stream.writeStartDocument();
    13. stream.writeStartElement(manifestNS, QString::fromLatin1("manifest"));
    14.  
    15. stream.writeEmptyElement(manifestNS, QString::fromLatin1("file-entry"));
    16. stream.writeAttribute(manifestNS, QString::fromLatin1("media-type"), QString::fromLatin1("application/vnd.oasis.opendocument.spreadsheet"));
    17. stream.writeAttribute(manifestNS, QString::fromLatin1("version"), QString::fromLatin1("1.2"));
    18. stream.writeAttribute(manifestNS, QString::fromLatin1("full-path"), QString::fromLatin1("/"));
    19. stream.writeEmptyElement(manifestNS, QString::fromLatin1("file-entry"));
    20. stream.writeAttribute(manifestNS, QString::fromLatin1("media-type"), QString::fromLatin1("text/xml"));
    21. stream.writeAttribute(manifestNS, QString::fromLatin1("full-path"), QString::fromLatin1("content.xml"));
    22. stream.writeEndElement(); // end manifest:manifest
    23. stream.writeEndDocument();
    To copy to clipboard, switch view to plain text mode 

    The result will be:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
    3. <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:version="1.2" manifest:full-path="/"/>
    4. <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
    5. </manifest:manifest>
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to vcp for this useful post:

    rakkar (30th October 2009)

Similar Threads

  1. Replies: 2
    Last Post: 21st August 2009, 23:09
  2. Saving hierarchical structures of data.
    By psih128 in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2009, 08:33
  3. Saving the widget class as a QPixmap
    By zgulser in forum Newbie
    Replies: 11
    Last Post: 2nd February 2009, 09:48
  4. Problem saving a plot
    By kalos80 in forum Qwt
    Replies: 2
    Last Post: 10th July 2008, 08:31
  5. Saving images from QTextEdit
    By borges in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 10:38

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.