Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qt6-changes.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page xml-changes-qt6.html
6 \title Changes to Qt XML
7 \ingroup changes-qt-5-to-6
8 \brief Use QXmlStreamReader for reading XML files.
9
10 Qt 6 is a result of the conscious effort to make the framework more
11 efficient and easy to use.
12
13 We try to maintain binary and source compatibility for all the public
14 APIs in each release. But some changes were inevitable in an effort to
15 make Qt a better framework.
16
17 In this topic we summarize those changes in Qt XML, and provide
18 guidance to handle them.
19
20 \section1 Simple API for XML (SAX) parser
21 All \b SAX classes have been removed from Qt XML. Use
22 QXmlStreamReader for reading XML files. Here are some simple steps to
23 port your current code to QXmlStreamReader:
24
25 For example, if you have code like
26
27 \code
28 QFile *file = new QFile(...);
29 QXmlInputSource *source = new QXmlInputSource(file);
30
31 Handler *handler = new Handler;
32
33 QXmlSimpleReader xmlReader;
34 xmlReader.setErrorHandler(handler);
35 xmlReader.setContentHandler(handler);
36
37 if (xmlReader.parse(source)) {
38 ... // do processing
39 } else {
40 ... // do error handling
41 }
42 \endcode
43
44 you can rewrite it as
45
46 \code
47 QFile file = ...;
48 QXmlStreamReader reader(&file);
49
50 while (!reader.atEnd()) {
51 reader.readNext();
52 ... // do processing
53 }
54 if (reader.hasError()) {
55 ... // do error handling
56 }
57 \endcode
58
59 \section2 QDom and QDomDocument
60
61 As \b SAX classes have been removed from Qt XML, QDomDocument
62 has been re-implemented using QXmlStreamReader.
63 This causes a few behavioral changes:
64
65 \list
66 \li Attribute values will be normalized. For example,
67 \c{<tag attr=" a \n b " />} is equivalent to \c{<tag attr="a b"/>}.
68 \li Identical qualified attribute names are no longer allowed. This
69 means attributes of an element must have unique names.
70 \li Undeclared namespace prefixes are no longer allowed.
71 \endlist
72
73 If you use QDomDocument and rely on any of these, you must update
74 your code and XML documents accordingly.
75
76 \section3 Spacing-only text nodes
77
78 By default, text nodes containing only spacing characters are stripped
79 and won't appear in the QDomDocument. The Qt 5 way of changing this behavior
80 was using the QDomDocument::setContent() overload that allowed a \c QXmlReader
81 to be supplied. That overload was removed in Qt 6.0, but since Qt 6.5,
82 you can pass QDomDocument::ParseOption::PreserveSpacingOnlyNodes as a parse
83 option, to specify that spacing-only text nodes must be preserved.
84
85 \section2 Qt Core5 compatibility library
86
87 If your application or library cannot be ported right now, the \l
88 QXmlSimpleReader and related classes still exist in Qt5Compat to keep
89 old code-bases working. If you want to use those SAX classes further, you
90 need to link against the new Qt5Compat module and add this line to your \l
91 qmake \c .pro file:
92
93 \code
94 QT += core5compat
95 \endcode
96
97 In case you already ported your application or library to the
98 \l {Build with CMake}{cmake} build system, add the following to your
99 \c CMakeList.txt:
100 \code
101 PUBLIC_LIBRARIES
102 Qt::Core5Compat
103 \endcode
104*/