Why is Qt losing my thin space unicode character when loading an XML file?
Question: I have an XML document, part of which has the following in it:

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

If this is loaded into Qt (specifically the Qt MathML widget where I found
this problem), the QDomDocument object loses the unicode thin space character
(U+2009). This Python example code demonstrates the problem:

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

The output from this code is:

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

Inserting an extra non-space character after the thin space stops the thin
space being lost.

Is this my mistake, an XML feature, or does Qt have a bug?


Answer: From [QDomDocument's documentation](http://qt-
project.org/doc/qt-4.8/qdomdocument.html):

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

So this way you do not lose the white space only data (example is in C++):

    
    
    QXmlSimpleReader reader;
    QXmlInputSource source;
    QDomDocument dom;
    
    source.setData(QString("<mtext>&#x2009;</mtext>"));
    dom.setContent(&source, &reader);
    

