[Qt-interest] Have QXmlStreameWriter write the encoding information

Andreas Pakulat apaku at gmx.de
Fri May 29 17:27:13 CEST 2009


On 29.05.09 16:13:49, Marc Balmer wrote:
>
> Am 29.05.2009 um 15:40 schrieb Andreas Pakulat:
>
>> On 29.05.09 14:19:57, Marc Balmer wrote:
>>>
>>> Am 29.05.2009 um 14:16 schrieb Marc Balmer:
>>>
>>>> Hi
>>>>
>>>> When I create a XML data stream using QXmlStreamWriter, my stream
>>>> starts with
>>>>
>>>> <?xml version="1.0"?>
>>>>
>>>> How can I add the encoding information, so that I get
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>
>>> I forgot to add my code, so here is the snippet:
>>>
>>>  QXmlStreamWriter xml(&xmlData);
>>> xml.setAutoFormatting(true);
>>> xml.setCodec("UTF-8");
>>> xml.writeStartDocument();
>>>
>>> xml.writeStartDocument() does not output the encoding information.
>>
>> Did you try with a different encoding? The reason it doesn't write the
>> encoding out could be that XML standard demands utf-8 to be used  
>> during
>> parsing if no encoding attribute is set. Hence the xml writer just  
>> avoids
>> "stating the obvious".
>
> that is not true.  If I set the codec to ISO-8859-1 and output Latin1  
> data, it still
> does not write the encoding.

Ah, so you're writing to a string, not a device (like a file)? If you look
into src/corelib/xml/qxmlstream.cpp and look for writeStartDocument you'll
notice that for string-output the writer won't add an encoding. This makes
sense because if it writes to a QString, then the xml will be unicode
anyway and you can convert it to something else than the encoding written
in the string afterwards too.

If you however use QByteArray or a real device, then you'll get the
encoding argument too.

Andreas

-- 
Your temporary financial embarrassment will be relieved in a surprising manner.



More information about the Qt-interest-old mailing list