When debugging XML code, you often wonder about the contents of a subtree. If the tree is small, you can just print it. But when the XML tree gets big, this just isn’t very handy. Especially not if you are looking at the output through a small debug window.

To view the contents of a subtree, you can use this simple function:

void printDomNode(const QDomNode& node) {
QDomDocument d("debugxml");
qDebug() << "Text XML:" << d.toString(); }

You will get the open XML tag at first, but since this is for debugging purposes, I never bothered to write the code to get rid of it. This trick is so small that I don’t have it stored anywhere, I just rewrite the function whenever I need to do it.

The only problem with this: It destroys the XML tree. Even though the node is a const ref, this code will still destroy the tree it comes from. I personally think this is a completely unacceptable Qt bug.