Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] QDomNode get formated text
Forum Updated to NodeBB v4.3 + New Features

[solved] QDomNode get formated text

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • patrikdP Offline
    patrikdP Offline
    patrikd
    wrote on last edited by patrikd
    #1

    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?
    Best,
    Patrik

    JonBJ 1 Reply Last reply
    0
    • patrikdP patrikd

      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?
      Best,
      Patrik

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @patrikd
      See the example at https://doc.qt.io/qt-5/qdomelement.html#text 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.

      1 Reply Last reply
      1
      • patrikdP Offline
        patrikdP Offline
        patrikd
        wrote on last edited by
        #3

        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);
                                concatedText.append(replacement);
                            }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");
                                    concatedText.append(replacement);
                                }
                            } else if (tag == "br"){
                                concatedText.append("<br/>");
                            } else if (chld.isText()){
                                QString txt = chld.toText().data();
                                concatedText.append(txt);
                            }
        
                            chld = chld.nextSibling();
                        }
                    }
        

        Best,
        Patrik

        JonBJ 1 Reply Last reply
        0
        • patrikdP patrikd

          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);
                                  concatedText.append(replacement);
                              }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");
                                      concatedText.append(replacement);
                                  }
                              } else if (tag == "br"){
                                  concatedText.append("<br/>");
                              } else if (chld.isText()){
                                  QString txt = chld.toText().data();
                                  concatedText.append(txt);
                              }
          
                              chld = chld.nextSibling();
                          }
                      }
          

          Best,
          Patrik

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

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

            QString output;
            QTextStream stream(&output);
            node.save(stream, 0);
            
            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved