#include #include #include #include #include #include static QByteArray Text(const QDomElement &elem) { QByteArray text{ elem.tagName().toUtf8() + "\n" }; for(QDomNode cur{elem.firstChild()}; !cur.isNull(); cur=cur.nextSibling()) { switch(cur.nodeType()) { case QDomNode::TextNode : text += cur.toText().data().toUtf8().toHex() + "\n"; break; case QDomNode::ElementNode : text += Text(cur.toElement()) + "\n"; break; } } return text; } int main(int argc, char *argv[]) { QApplication app(argc,argv); // document without "only spacing" elements QDomDocument docu; QDomElement root{ docu.createElement(QStringLiteral("root")) }; QDomElement elema{ docu.createElement(QStringLiteral("A")) }; elema.appendChild( docu.createTextNode(QStringLiteral("1")) ); // in hex: 0x31 root.appendChild(elema); QDomElement elemb{ docu.createElement(QStringLiteral("B")) }; elemb.appendChild( docu.createTextNode(QStringLiteral(" ")) ); // just spaces, 0x20 root.appendChild(elemb); docu.appendChild(root); // read document into another document QDomDocument read; const QByteArray data{ docu.toByteArray(2) }; // notice the two spaces in as textNode data // comment out either line below to test const QDomDocument::ParseOption parseoption = // QDomDocument::ParseOption::Default; // filters out the all-spaces node, and does not add phantom nodes QDomDocument::ParseOption::PreserveSpacingOnlyNodes; // preserves spacing only node, but adds phantom nodes read.setContent(data,parseoption); // content of text nodes as text const QByteArray text{ Text(read.documentElement()) }; // show text QLabel label{ text }; label.show(); return QApplication::exec(); }