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");
doc.appendChild(root);
QDomElement testElement = doc.createElement("second_test");
root.appendChild(testElement);@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");
@Thanks,
marsupial -
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,
"http://doc.qt.nokia.com/4.7/qdomdocument.html#details":http://doc.qt.nokia.com/4.7/qdomdocument.html#details
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.
@ctor(){
_doc = new QDomDocument();
}~(){
delete _doc;
}//Type is enum.
QDomNode* class::createNode(QDomNode* parent, const QString& nodeText, Type type)
{switch(type){ case Element: QDomElement* elem = _doc->createElement(nodeText); parent->appendChild(*elem); return elem; ... }
}@