[Interest] QDateTime + QXmlStreamWriter on non-UTF8 compatible systems

Scott Bloom scott at towel42.com
Fri Dec 22 20:21:24 CET 2017


-----Original Message-----
From: Thiago Macieira [mailto:thiago.macieira at intel.com] 
Sent: Friday, December 22, 2017 4:10 AM
To: interest at qt-project.org
Cc: Scott Bloom <scott at towel42.com>
Subject: Re: [Interest] QDateTime + QXmlStreamWriter on non-UTF8 compatible systems

On quinta-feira, 21 de dezembro de 2017 20:49:33 -02 Scott Bloom wrote:
> When using the QXmlStreamWriter to write out an XML file, where one of 
> the sections is a DateTime Field, and the system is non-UTF8 (Japanese 
> in my customer case) the XML generated via
> 
> writer.writeCharacters( dateTime.toString() )
> 
> Produces XML that cant be read in via QXmlStreamReader

What is your Writer writing to?

If it's a QIODevice or QByteArray, then it does this:
        QByteArray bytes = encoder->fromUnicode(s);
        device->write(bytes)

If it's a string, it does:
        stringDevice->append(s);

(this is after escaping)

What is the XML it generates? We need to isolate the problem either in the Writer or in the Reader.
======================
I was able to track down the issue. 

Typically, I was writing to a QString, for some reason, there is an undocumented difference in the writer for QString vs QIODevice writing.

>From QXmlStreamWriter::writeStartDocument

    if (d->device) { // stringDevice does not get any encoding
        d->write("\" encoding=\"");
#ifdef QT_NO_TEXTCODEC
        d->write("iso-8859-1");
#else
        d->write(d->codec->name().constData(), d->codec->name().length());
#endif
    }

The codec was not getting written out since I was using a QString, by changing to a QBuffer, the encoding is written out, and the XML can be read in properly.

Note, according to the documentation there is NO differentiation of "String based" vs "Non String based"
void QXmlStreamWriter::writeStartDocument()
This is an overloaded function.

Writes a document start with XML version number "1.0". This also writes the encoding information.

This function was introduced in Qt 4.5.

Frankly, I am not sure why this difference exists, but it should definitely be documented.

Scott



More information about the Interest mailing list