Results 1 to 2 of 2

Thread: How can I read an xml utf-16 file with QXmlStreamReader?

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

    Default How can I read an xml utf-16 file with QXmlStreamReader?

    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I read an xml utf-16 file with QXmlStreamReader?

    A file is just a stream of bytes to a QIODevice.

    This code:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QFile in("utf16test.xml");
    9. in.open(QIODevice::ReadOnly);
    10. QXmlStreamReader xml(&in);
    11. while (!xml.atEnd()) {
    12. xml.readNextStartElement();
    13. qDebug() << xml.name();
    14. }
    15. if (xml.hasError()) {
    16. // do error handling
    17. }
    18.  
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    handles UTF16 encoded XML files just fine here. The reader code includes detection of UTF 16 or 32, BE or LE, with or without BOM, and falls back to UTF-8 based on the first four bytes of the XML file.

Similar Threads

  1. Reading and rereading a file with QXmlStreamReader
    By TheRonin in forum Qt Programming
    Replies: 14
    Last Post: 30th April 2015, 14:04
  2. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  3. QXmlStreamReader - read a sub document
    By elcuco in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2010, 15:44
  4. Replies: 3
    Last Post: 3rd April 2010, 20:35
  5. 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

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.