A file is just a stream of bytes to a QIODevice.
This code:
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv)
{
QFile in
("utf16test.xml");
QXmlStreamReader xml(&in);
while (!xml.atEnd()) {
xml.readNextStartElement();
qDebug() << xml.name();
}
if (xml.hasError()) {
// do error handling
}
return 0;
}
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QFile in("utf16test.xml");
in.open(QIODevice::ReadOnly);
QXmlStreamReader xml(&in);
while (!xml.atEnd()) {
xml.readNextStartElement();
qDebug() << xml.name();
}
if (xml.hasError()) {
// do error handling
}
return 0;
}
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.
Bookmarks