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

All SAX classes have been removed from Qt XML. 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

As SAX classes have been removed from Qt XML, QDomDocument has been re-implemented using QXmlStreamReader. This causes a few behavioral changes:

  • Attribute values will be normalized. For example, <tag attr=" a \n b " /> is equivalent to <tag attr="a b"/>.
  • Identical qualified attribute names are no longer allowed. This means attributes of an element must have unique names.
  • Undeclared namespace prefixes are no longer allowed.

If you use QDomDocument and rely on any of these, you must update your code and XML documents accordingly.

Spacing-only text nodes

By default, text nodes containing only spacing characters are stripped and won't appear in the QDomDocument. The Qt 5 way of changing this behavior was using the QDomDocument::setContent() overload that allowed a QXmlReader to be supplied. That overload was removed in Qt 6.0, but since Qt 6.5, you can pass QDomDocument::ParseOption::PreserveSpacingOnlyNodes as a parse option, to specify that spacing-only text nodes must be preserved.

Qt Core5 compatibility library

If your application or library cannot be ported right now, the QXmlSimpleReader and related classes 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

© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.