qt4qt4.7qtxml

Create QDomNodes without QDomDocument


Is it possible to create a QDomElement without having a QDomDocument available? For example, here is a function that is expected to build a node tree under the element parent:

void buildResponse (QDomDocument &doc, QDomElement &parent) {
    QDomElement child = doc.createElement("child");
    parent.appendChild(child);
}

The only reason I have to pass doc is to use it as a factory to create elements that the function adds under parent. In the application I'm working on now, it would simplify my implementation slightly if I didn't have to lug the QDomDocument around.

Is there a way to create nodes without having a document available?


Solution

  • You can drop document as parameter because each QDomNode has method ownerDocument(). QDomElement inherits QDomNode so it's also accessible from parent parameter. Check QDomNode documentation.