Results 1 to 4 of 4

Thread: QXmlStreamWriter always saves my doc in UTF-16

  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QXmlStreamWriter always saves my doc in UTF-16

    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:

    Qt Code:
    1. FF FE 3C 00 3F 00 78 00 6D 00 6C 00 <?xml
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QFile fout(m_filename);
    2.  
    3. if( !fout.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
    4. return;
    5.  
    6. QXmlStreamWriter xml(&fout);
    7. xml.setCodec("UTF-8");
    8. if( m_chain->serialize(&xml) ) {
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

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

    Is there something I forgot to set up ?

    Thanks for your advices.

    Z.

  2. #2
    Join Date
    May 2011
    Posts
    6
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: QXmlStreamWriter always saves my doc in UTF-16

    You may test if the codec is available:

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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QXmlStreamWriter always saves my doc in UTF-16

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QXmlStreamWriter always saves my doc in UTF-16

    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.

Similar Threads

  1. write new line with qxmlstreamwriter using &#xD;
    By jh in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2012, 20:05
  2. QXmlStreamWriter and while/if loop
    By prophet0 in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2012, 22:18
  3. QDomDocument or QXmlStreamWriter, QXmlStreamReader
    By calmspeaker in forum Qt Programming
    Replies: 6
    Last Post: 15th December 2010, 09:57
  4. QXmlStreamWriter UTF-16 Problem
    By 27Loco in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2010, 13:04
  5. Readable Xml with QXmlStreamWriter
    By jano_alex_es in forum Newbie
    Replies: 8
    Last Post: 26th August 2009, 11:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.