为什么Qt在加载XML文件时会丢失我的精简空间unicode字符?

前端之家收集整理的这篇文章主要介绍了为什么Qt在加载XML文件时会丢失我的精简空间unicode字符?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 XML文档,其中一部分包含以下内容

<math display='block'><mtext>&#x2009;</mtext></math>

如果将其加载到Qt(特别是我发现此问题的Qt MathML小部件),则QDomDocument对象将丢失unicode精简空格字符(U 2009).这个Python示例代码演示了这个问题:

from PyQt4.QtXml import *

d = QDomDocument()
d.setContent("<math display='block'><mtext>&#x2009;</mtext></math>")
print repr(unicode(d.toString()))

代码输出是:

u'<math display="block">\n <mtext/>\n</math>\n'

在精简空间之后插入额外的非空格字符会使精简空间丢失.

这是我的错误,XML功能,还是Qt有错误

解决方法

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>&#x2009;</mtext>"));
dom.setContent(&source,&reader);

猜你在找的XML相关文章