Changes to Qt XML

Qt 6 is a result of the conscious effort to make the framework more efficient and easy to use.

We try to maintain binary and source compatibility for all the public APIs in each release. But some changes were inevitable in an effort to make Qt a better framework.

In this topic we summarize those changes in Qt XML, and provide guidance to handle them.

Simple API for XML (SAX) parser

QXmlStreamReader

In Qt6, all SAX classes have been removed from Qt XML, please use QXmlStreamReader for reading XML files. Here are some simple steps to port your current code to QXmlStreamReader:

For example, if you have code like

QFile *file = new QFile(...);
QXmlInputSource *source = new QXmlInputSource(file);

Handler *handler = new Handler;

QXmlSimpleReader xmlReader;
xmlReader.setErrorHandler(handler);
xmlReader.setContentHandler(handler);

if (xmlReader.parse(source)) {
    ... // do processing
} else {
    ... // do error handling
}

you can rewrite it as

QFile file = ...;
QXmlStreamReader reader(&file);

while (!reader.atEnd()) {
    reader.readNext();
    ... // do processing
}
if (reader.hasError()) {
    ... // do error handling
}

QDom and QDomDocument

In Qt6, SAX classes have been removed and therefore QDomDocument cannot use them anymore. That's why it has been re-implemented using the QXmlStreamReader. This brings a few behavioral changes:

  • Attribute values will be normalized. For example <tag attr=" a \n b " /> will be equivalent to <tag attr="a b"/>.
  • Identical qualified attribute names won't be allowed anymore, i.e. attributes of an element must have unique names.
  • Undeclared namespace prefixes won't be allowed anymore.

If you are using QDomDocument and relying on any of these, please update your code and XML documents accordingly.

Qt Core5 compatibility library

If your application or library cannot be ported right now, the QXmlSimpleReader and related classes do still exist in Qt5Compat to keep old code-bases working. If you want to use those SAX classes further, you need to link against the new Qt5Compat module and add this line to your qmake .pro file:

QT += core5compat

In case you already ported your application or library to the cmake build system, add the following to your CMakeList.txt:

PUBLIC_LIBRARIES
    Qt::Core5Compat

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.0/xml-changes-qt6.html