PDA

View Full Version : Can't read xml file with QXmlSreamReader that has utf-16 encoding



SuperHannaR
31st July 2012, 12:12
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

yeye_olive
31st July 2012, 13:19
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.:

QFile file("myFile.xml");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Cannot open file.";
return;
}
QXmlStreamReader reader(&file);
...
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?