c++qtqtxml

How to make a deep copy of an entire QDomDocument


I want to create a deep copy of a QDomDocument in an xml model in order to allow the user to later restore the document to its original state. The QDomDocument documentation says that this can be achieved by using cloneNode(). However, cloneNode() returns a QDomNode, not a QDomDocument, and I can't seem to figure out how how to properly add it to a new document.

I've tried:

QDomDocument copy;
copy.importNode(existingDocument.cloneNode(true),true);

and

QDomDocument copy;
copy.appendChild(existingDocument.cloneNode(true),true);

but neither work.


Solution

  • If you have a QDomNode, you can use its toDocument function.

    Assuming QDomNode node is the node returned from cloneNode()

    QDomDocument newDocument = node.toDocument();