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. QDomDocument toString conversion issue
Forum Updated to NodeBB v4.3 + New Features

QDomDocument toString conversion issue

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 3.1k Views 2 Watching
  • 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.
  • L Offline
    L Offline
    Lolo67
    wrote on last edited by
    #1

    In the following example

    QDomDocument dom;
    QDomElement element= dom.createElement("MyElement");
    element.setAttribute("attr1", "foo");
    element.setAttribute("attr2", "bar");
    element.setAttribute("attr3", "<<statistics>> Integer status");
    dom.appendChild(element);
    
    QString text = dom.toString();
    qDebug() << QString("text = %1").arg(text);
    
    QFile outFile("C:/temp/testFile.dcf");
    if ( outFile.open(QIODevice::WriteOnly|QIODevice::Text))
    {
      QTextStream stream( &outFile );
      stream << text << endl;
      outFile.close();
    }
    

    The produced file will contain:

    <MyElement attr2="bar" attr1="foo" attr3="&lt;&lt;statistics>> Integer status"/>
    

    While I am awaiting it to contain:

    <MyElement attr2="bar" attr1="foo" attr3="<<statistics>> Integer status"/>
    

    Does anyone know how this could be addressed?

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Some characters must be escaped in XML documents

      "   &quot;
      '   &apos;
      <   &lt;
      >   &gt;
      &   &amp;
      

      https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents

      It should turn into correct string once read back.

      maybe

      element.setAttribute("attr3", "\<\<statistics\>\> Integer status");
      

      will work but I didnt test it.

      1 Reply Last reply
      1
      • L Offline
        L Offline
        Lolo67
        wrote on last edited by
        #3

        Thanks for the reply.

        Well I could understand that some characters must be escaped, but I do not understand the logic behind Qt escaping some of them but not all.

        In my example I am passing the string: <<statistics>> Integer status
        Qt is escaping the '<' characters but not the '>' characters composing the string.

        Also your suggestion to write:

        element.setAttribute("attr3", "\<\<statistics\>\> Integer status");
        

        will not work for me. The code I gave here was just an example to show what I was observing. I am actually reading several XML files and use the content found in these files and combine those to create some new XML files. The "<<statistics>> Integer status" string is something I do find in the input XML files, and I would like to have this string appearing again in the XML files I am generating with my application.

        mrjjM 1 Reply Last reply
        1
        • L Lolo67

          Thanks for the reply.

          Well I could understand that some characters must be escaped, but I do not understand the logic behind Qt escaping some of them but not all.

          In my example I am passing the string: <<statistics>> Integer status
          Qt is escaping the '<' characters but not the '>' characters composing the string.

          Also your suggestion to write:

          element.setAttribute("attr3", "\<\<statistics\>\> Integer status");
          

          will not work for me. The code I gave here was just an example to show what I was observing. I am actually reading several XML files and use the content found in these files and combine those to create some new XML files. The "<<statistics>> Integer status" string is something I do find in the input XML files, and I would like to have this string appearing again in the XML files I am generating with my application.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Lolo67
          Well maybe you can use

          QString plain = "#include <QtCore>"
          QString html = plain.toHtmlEscaped();

          // html == "#include &lt;QtCore&gt;"
          

          it seems to be a known issue with QDomDocument that not all are automatically escaped but i didnt not find further info on why.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lolo67
            wrote on last edited by
            #5

            Is there a way to force the characters NOT to be escaped ?

            mrjjM 1 Reply Last reply
            0
            • L Lolo67

              Is there a way to force the characters NOT to be escaped ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Lolo67
              Not that I have seen.
              Also that would be invalid XML.

              1 Reply Last reply
              2
              • G Offline
                G Offline
                gigglesun
                wrote on last edited by
                #7

                Did you solved the problem, I met the same issue also.

                mrjjM 1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @gigglesun said in QDomDocument toString conversion issue:

                  I met the same issue also.

                  Which problem? QtXml will not create invalid xml.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • G gigglesun

                    Did you solved the problem, I met the same issue also.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @gigglesun
                    Hi
                    Can you explain why you want to generate invalid xml ?
                    Some characters must be escaped or it cant be parsed.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gigglesun
                      wrote on last edited by gigglesun
                      #10

                      @Christian-Ehrlicher @mrjj , My problem is the same with @Lolo67 in his topic, to make it more clear, I change the example as below:

                          QDomDocument dom;
                          QString text ="<MyElement> a &gt; b and c &lt; d</MyElement>";
                          dom.setContent(text);
                      
                          QString verifyText = dom.toString();
                          qDebug() << verifyText; // "<MyElement> a > b and c &lt; d</MyElement>"
                      
                      

                      My problem is why the text set to the QDomDocument is different with what we get from it.
                      Also, it's strange that Qt is replacing the '&gt' characters to '>' but not the '&lt' characters to '<', in fact, I do not want these change happen.
                      My QT version is 4.8.7, not sure if this is related with QT version.

                      update:
                      Found it's a known issue reported in https://bugreports.qt.io/browse/QTBUG-14690, till now, it's not fixed.

                      1 Reply Last reply
                      0

                      • Login

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