[Qt-interest] Have QXmlStreameWriter write the encoding information

Marc Balmer marc at msys.ch
Wed Jun 3 09:24:50 CEST 2009


Am 03.06.2009 um 09:06 schrieb Andreas Pakulat:

> On 02.06.09 23:01:37, Lingfa Yang wrote:
>> Marc Balmer wrote:
>>> Am 29.05.2009 um 16:13 schrieb Marc Balmer:
>>>
>>>
>>>> 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.
>>>>
>>>
>>> Soe does anyone on this list have an idea how I can get the encoding
>>> information to be written?  Or is it time to file a bug report?
>>>
>> What you want is a processing instruction line, so you should call
>> writeProcessingInstruction(), i.e.
>>    xml.writeProcessingInstruction("xml", "version=\"1.0\"
>> encoding=\"UTF-8\"");
>
> That will produce
>
> <?xml version="1.0"?><?xml version="1.0" encoding="UTF-8"?>
>
> so definetly not what he wants.
>
> If you're writing into a QString there's simply no point in adding an
> encoding, because the encoding is dictated by QString.

actually yes, there is a point.  namely if you use the QString only  
temporarely
while composing the XML content and then later output it as ISO-8859-1
using QString::toLatin1().  This does not work,

Imo, Qt does to much magic here.  But then I found a way to work  
around it,
so my problem is definitely gone.

- Marc Balmer

>
> Andreas
>
> -- 
> Good night to spend with family, but avoid arguments with your mate's
> new lover.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list