Changes to Qt XML

Qt 6 是努力使框架更高效,且更易于使用的结果。

为兼容每个发行的所有公共 API,我们试着维护二进制和源代码。但是,为使 Qt 成为更优框架,一些改变是不可避免的。

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

Simple API for XML (SAX) parser

所有 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 :

例如,若有代码像

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
}
					

可以把它重写成

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 " /> 相当于 <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.

若使用 QDomDocument and rely on any of these, you must 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 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 文件:

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