我有一个
XML文档,其中一部分包含以下内容:
<math display='block'><mtext> </mtext></math>
如果将其加载到Qt(特别是我发现此问题的Qt MathML小部件),则QDomDocument对象将丢失unicode精简空格字符(U 2009).这个Python示例代码演示了这个问题:
from PyQt4.QtXml import * d = QDomDocument() d.setContent("<math display='block'><mtext> </mtext></math>") print repr(unicode(d.toString()))
u'<math display="block">\n <mtext/>\n</math>\n'
在精简空间之后插入额外的非空格字符会使精简空间丢失.
解决方法
从
QDomDocument’s documentation开始:
Text nodes consisting only of whitespace are stripped and won’t appear
in the QDomDocument. If this behavior is not desired,one can use the
setContent() overload that allows a QXmlReader to be supplied.
所以这样你就不会丢失只有空格的数据(例如C中):
QXmlSimpleReader reader; QXmlInputSource source; QDomDocument dom; source.setData(QString("<mtext> </mtext>")); dom.setContent(&source,&reader);