⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

DOM classes: Question about the design

  • Hi,

    I have a question regarding the class design of the Qt DOM classes:

    Usually I create a document like this:

    @QDomDocument doc;
    QDomElement root = doc.createElement("test");
    QDomElement testElement = doc.createElement("second_test");

    It is a little annoying having to call the appendChild method every time after I create an element. Why is the element not added to the parent element or the doc when it is created? Wouldn't that make more sense?
    What is the reason for the class design the qt developers used?

    I think this would look much nicer:

    @QDomDocument doc;
    QDomElement root = doc.createElement("test");
    QDomElement testElement = root.createElement("second_test");


  • Your proposal duplicates functionlity in two different classes.
    In QDomDocument and in QDomElement. If gone with your approach, it'd further require duplication in QDomComment, QDomCDATASection, etc...

    Also, another philosophical reason is clearly mentioned in the QDomDocument that,


    bq. Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an ownerDocument() function which associates them with the document within whose context they were created.

    Make yourself a little helper class based on below pseudo.

    _doc = new QDomDocument();

    delete _doc;

    //Type is enum.
    QDomNode* class::createNode(QDomNode* parent, const QString& nodeText, Type type)

        case Element:
             QDomElement* elem = _doc->createElement(nodeText);
             return elem;


Log in to reply