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="<<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?
-
Hi
Some characters must be escaped in XML documents" " ' ' < < > > & &
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.
-
@Lolo67
Well maybe you can useQString plain = "#include <QtCore>"
QString html = plain.toHtmlEscaped();// html == "#include <QtCore>"
it seems to be a known issue with QDomDocument that not all are automatically escaped but i didnt not find further info on why.
-
@gigglesun said in QDomDocument toString conversion issue:
I met the same issue also.
Which problem? QtXml will not create invalid xml.
-
@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 > b and c < d</MyElement>"; dom.setContent(text); QString verifyText = dom.toString(); qDebug() << verifyText; // "<MyElement> a > b and c < 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 '>' characters to '>' but not the '<' 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.