PDA

View Full Version : QXmlStreamWriter always saves my doc in UTF-16



zyend
24th March 2014, 16:04
Hello,

I have recently used the QXmlStreamWriter class to generate my xml trees. In Qt documentation, it is explained that QXmlStreamWriter uses UTF-8 by default.
However, if I open the resulting file in a hex editor, it looks like the xml is in UTF-16 instead.

In the hex editor, I have the following caracters:


FF FE 3C 00 3F 00 78 00 6D 00 6C 00 <?xml

I noticed "FF FE" as the UTF-16 BOM as well as the file size which is twice the number of characters.

I changed my code in order to force the UTF-8 as follow:


QFile fout(m_filename);

if( !fout.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
return;

QXmlStreamWriter xml(&fout);
xml.setCodec("UTF-8");
if( m_chain->serialize(&xml) ) {
...
}

But when I open the xml file in the hex editor, I still have the same result. :confused:

Is there something I forgot to set up ?

Thanks for your advices.

Z.

derhexer
24th March 2014, 16:11
You may test if the codec is available:

QTextCodec * QTextCodec::codecForName(const char * name) [static]

wysota
24th March 2014, 16:37
Please provide a minimal compilable example reproducing the problem.

ChrisW67
24th March 2014, 20:24
The problem is in your serialize() function. QXmlStreamWriter does not write a file size into the stream. It looks as if you have somehow mashed binary data produced with QDataStream into the XML stream.