Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDomDocument toString conversion issue



  • 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?


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



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


  • Qt Champions 2019

    @gigglesun said in QDomDocument toString conversion issue:

    I met the same issue also.

    Which problem? QtXml will not create invalid xml.


  • Lifetime Qt Champion

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



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


Log in to reply