Results 1 to 2 of 2

Thread: Can't read xml file with QXmlSreamReader that has utf-16 encoding

  1. #1
    Join Date
    Jan 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't read xml file with QXmlSreamReader that has utf-16 encoding

    I try to find a way to read an xml file that has utf-16 encoding, apparently QIODevice can’t read utf-16 xml files, it says it’s invalid file.

    After searching the internet and the t assistant I found that the only library that can set a codec in a file is QTextStream.

    Then I found this: QTextCodec.

    After searching a bit more on the internet I understood that I need to create an encoding function by myself that should look like this:

    QByteArray myEncoderFunc( const QString &xmlfileNameSt );

    And call this function like this:

    xmlFile.setEncodingFunction ( myEncoderFunc ) ;

    With all this information I don’t really know how do I make my file readable for QXmlStreamReader.

    Can you help in this?

    Thanks,
    Hanna

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't read xml file with QXmlSreamReader that has utf-16 encoding

    I think you are confusing several things here. QFile::setEncodingFunction() is for encoding file names (which you need not care about), while you seem to be interested in decoding file contents.

    Normally QXmlStreamReader should work without problem if the file is well formed. E.g.:
    Qt Code:
    1. QFile file("myFile.xml");
    2. if (!file.open(QIODevice::ReadOnly)) {
    3. qDebug() << "Cannot open file.";
    4. return;
    5. }
    6. QXmlStreamReader reader(&file);
    7. ...
    To copy to clipboard, switch view to plain text mode 
    What do you mean by UTF-16? Is it UTF-16LE, or UTF-16BE, or does it use a byte-order mark? Can you confirm that your file's encoding matches the value of the "encoding" attribute of the XML declaration at the beginning of the file?

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 15:42
  3. Replies: 1
    Last Post: 20th June 2008, 18:43
  4. Read An Xml File
    By Alienxs in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2007, 00:28
  5. How to read encoding type in XML file
    By danbr in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2006, 08:17

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
  •  
Qt is a trademark of The Qt Company.