Nominate our 2022 Qt Champions!

[solved] QDomNode get formated text

  • Hi all,
    I'm currently parsing a xml file. I get the content of the QDomeNodes with node.toElement().text(). So far so good.
    But I have several nodes which have some formating tags. Those are stripped by the toElement().text() function.
    Is there a way to get the content of a node with those informations?

  • @patrikd
    See the example at for how only the text part is returned.

    It only evaluates QDomText and QDomCDATASection objects

    You must iterate/recurse through all QDomNode children (QDomNode::childNodes()) if you want to visit all non-pure-text nodes.

    EDIT So, to be clear. Totally untested by me, but if you have, say, <b>Bold</b> then QDomElement::text() will only return Bold, which is the only text within the node/element. But if you visit the descendants individually you should find you have a QDomElement with nodeName() == "b" and that has a child QDomText with nodeValue()/text() == "Bold". You can use that nodeName() == "b" if you want to recognise the bold tag or reconstruct the <b>...</b> for yourself.

  • Hi JonB,
    thx for the tip. Works fine for me :)
    And for those who run into the same:

                QString concatedText = "";
                if (node.hasChildNodes()){
                    concatedText = "";
                    QDomNode chld = node.firstChild();
                    QString tagText = "<%1>%2</%3>";
                    while (!chld.isNull()) {
                        QString tag = chld.nodeName();
                        if (tag == "b" || tag == "i" || tag == "u"){
                            QString val = chld.toElement().text();
                            QString replacement = tagText.arg(tag).arg(val).arg(tag);
                        }else if (tag == "emph"){
                            QString attr = chld.toElement().attribute("type").trimmed();
                            if (attr == "under"){
                                QString val = chld.toElement().text();
                                QString replacement = tagText.arg("u").arg(val).arg("u");
                        } else if (tag == "br"){
                        } else if (chld.isText()){
                            QString txt = chld.toText().data();
                        chld = chld.nextSibling();


  • @patrikd
    Looking at your code, I wonder if you are trying to reinvent the wheel! :) You seem to be just going through a QDomNode (or whatever) and reconstructing the HTML which did/could have generated it. Are you aware that, say, QString QDomDocument::toString() can be used the other way round, to turn a dom document back to corresponding HTML, instead of you trying to do the (incomplete) work?

    Converts the parsed document back to its textual representation.

  • If you just need something like outerHTML in javascript, you can use QDomNode::save like:

    QString output;
    QTextStream stream(&output);, 0);

Log in to reply