Important: Please read the Qt Code of Conduct -

The most elegant way to handle xml serialization

  • What's in your opinion the most elegant way to handle xml serialization in Qt?

    Should I use QDomDocument and similar classes from the same group or maybe QXmlStreamReader, QXmlStreamWriter and linearly serializing, deserializing my custom class? I'd like to note that my class is not QObject class.

    I need simple save and load in classes I need to serialize and deserialize and nothing more.

    Should I create some interface (pure-abstract class) for that?

  • Lifetime Qt Champion

    For a larger number of classes that need serialization, i prefer a
    base class design with virtual functions allowing for polymorph saving and loading. This base class also give helper functions to save/load a vectors and other convenience functions to minimize the amount of code needed in the child/using classes.

    However, for a few classes, nothing wrong with just save / load directly.

    I like QDomDocument interface and its easy to construct the tree but it can also be a bit verbose.
    QXmlStreamWriter can look really tight but could be messy for
    deep/complex structures as its more flatten in expressing the actual (data) tree. (IMHO)
    If we go with "KISS", i would go with QXmlStreamWriter and check out how it felt with a few test classes.

     stream.writeAttribute("href", "");
     stream.writeTextElement("title", "Qt Home");
     stream.writeEndElement(); // bookmark

Log in to reply