Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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 and align with modern standards.
16
17 Qt 6 enforces XML 1.0 rules more strictly than Qt 5. In Qt 5,the XML
18 parser was more lenient, allowing certain constructs that were not
19 compliant with the XML 1.0 specification.
20 Qt 6 corrects this behavior, ensuring that XML handling follows
21 the standard properly. If your application relies on behavior
22 that was incorrectly permitted in Qt 5, you may need to adjust your
23 XML documents or processing logic accordingly.
24
25 For more details on XML 1.0 rules, see the official W3C XML specification:
26 \l {https://www.w3.org/TR/xml/} {Extensible Markup Language (XML) 1.0 (Fifth Edition)}
27
28 In this topic we summarize those changes in Qt XML, and provide
29 guidance to handle them.
30
31 \section1 Simple API for XML (SAX) parser
32 All \b SAX classes have been removed from Qt XML. Use
33 QXmlStreamReader for reading XML files. Here are some simple steps to
34 port your current code to QXmlStreamReader:
35
36 For example, if you have code like
37
38 \code
39 QFile *file = new QFile(...);
40 QXmlInputSource *source = new QXmlInputSource(file);
41
42 Handler *handler = new Handler;
43
44 QXmlSimpleReader xmlReader;
45 xmlReader.setErrorHandler(handler);
46 xmlReader.setContentHandler(handler);
47
48 if (xmlReader.parse(source)) {
49 ... // do processing
50 } else {
51 ... // do error handling
52 }
53 \endcode
54
55 you can rewrite it as
56
57 \code
58 QFile file = ...;
59 QXmlStreamReader reader(&file);
60
61 while (!reader.atEnd()) {
62 reader.readNext();
63 ... // do processing
64 }
65 if (reader.hasError()) {
66 ... // do error handling
67 }
68 \endcode
69
70 \section2 QDom and QDomDocument
71
72 As \b SAX classes have been removed from Qt XML, QDomDocument
73 has been re-implemented using QXmlStreamReader.
74 This causes a few behavioral changes:
75
76 \list
77 \li Attribute values will be normalized. For example,
78 \c{<tag attr=" a \n b " />} is equivalent to \c{<tag attr="a b"/>}.
79 \li Identical qualified attribute names are no longer allowed. This
80 means attributes of an element must have unique names.
81 \li Undeclared namespace prefixes are no longer allowed.
82 \endlist
83
84 For more details see:
85 \l {https://www.w3.org/TR/xml/#AVNormalize} {Attribute-Value Normalization in XML 1.0}
86
87 \section2 Control characters
88
89 In Qt 6, control characters such as U+0000—U+001F, U+007F, and U+0080—U+009F
90 are now correctly rejected per XML 1.0 rules.
91 They were incorrectly allowed in Qt 5.
92 Before using XML with Qt6 ensure that your XML documents contain only valid
93 XML 1.0-compliant characters. If control characters are necessary, encode
94 them using text-safe format.
95
96 For more details see:
97 \l {https://www.w3.org/TR/xml/#charsets} {Characters in XML 1.0}
98
99 \section2 HTML Entities in XML
100
101 In Qt 6, HTML entities are no longer valid unless explicitly declared in a
102 Document Type Definition (DTD). In Qt 5, certain HTML-specific entities
103 (e.g., \c{&nbsp;}) were allowed, even without declaration. To ensure
104 compatibility in Qt 6 use numeric character references, define required
105 entities in a DTD or if your content relies on HTML entities, process
106 the XML as HTML instead.
107
108 For more details see:
109 \l {https://www.w3.org/TR/xml/#charencoding} {Character Encoding in Entities in XML 1.0}
110
111 \section2 Spacing-only text nodes
112
113 By default, text nodes containing only spacing characters are stripped
114 and won't appear in the QDomDocument. The Qt 5 way of changing this behavior
115 was using the QDomDocument::setContent() overload that allowed a \c QXmlReader
116 to be supplied. That overload was removed in Qt 6.0, but since Qt 6.5,
117 you can pass QDomDocument::ParseOption::PreserveSpacingOnlyNodes as a parse
118 option, to specify that spacing-only text nodes must be preserved.
119
120 If you use QDomDocument and rely on any of these, you must update
121 your code and XML documents accordingly.
122
123 \section1 Qt Core5 compatibility library
124
125 If your application or library cannot be ported right now, the \l
126 QXmlSimpleReader and related classes still exist in Qt5Compat to keep
127 old code-bases working. If you want to use those SAX classes further, you
128 need to link against the new Qt5Compat module and add this line to your \l
129 qmake \c .pro file:
130
131 \code
132 QT += core5compat
133 \endcode
134
135 In case you already ported your application or library to the
136 \l {Build with CMake}{cmake} build system, add the following to your
137 \c CMakeList.txt:
138 \code
139 PUBLIC_LIBRARIES
140 Qt::Core5Compat
141 \endcode
142*/