Parsing XML with Qt: Updates for Qt 6
It's been a while since our last posts in regards to parsing XML documents with Qt. I'd like to give some updates about the expected changes in the upcoming releases. But first, let's summarize what Qt currently provides for reading and writing XML documents.
Qt XML module
This module provides implementations for two different models for reading and writing XML files: Document Object Model (DOM) and Simple API for XML (SAX). With DOM model the full XML file is loaded in memory and represented as a tree, this allows easy access and manipulation of its nodes. DOM is typically used in applications where you don't care that much about memory. SAX, on the other hand, is an event based XML parser and doesn't load the whole XML document into memory. Instead it generates events for tokens while parsing, and it's up to the user to handle those events. The application has to implement the handler interfaces (fully, or partially by using
QXmlDefaultHandler). A lot of people find this inconvenient as it forces them to structure their code around this model.
Another problem is that the current implementation of SAX (and as a consequence DOM, since it's implemented using SAX) is not fully compliant with the XML standard. Considering these downsides, Qt does not recommend using SAX anymore, and the decision has been made to deprecate those classes starting from Qt 5.15.
QXmlStreamReader and QXmlStreamWriter
Luckily Qt provides XML streaming classes as a more convenient and XML-standard compliant alternative for working with XML files. You can also check out this blog post to get a quick introduction to XML streaming with Qt.
What will change in Qt 6?
As it is mentioned above, SAX classes will be deprecated soon, which means that
QDomDocument cannot use them anymore. That's why it has been re-implemented using the
QXmlStreamReader. Qt 6 will switch to the new implementation, but the older Qt versions will still use the old implementation, since the new one brings a few behavioral changes, and we don't want to do this before Qt 6.
What does this change mean for Qt DOM users? Since
QXmlStreamReader is following the XML specification more strictly,
QDomDocument will do the same starting from Qt 6. This will imply the following behavior changes for
- 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 consider updating your code and XML documents accordingly.
It would be nice to hear your feedback on how do you currently use Qt’s XML features. Is there anything missing you would like to see? Please let us know in the comments!
Subscribe to our newsletter
Try Qt 6.3 Now!
Download the latest release here: www.qt.io/download.
Qt 6 was created to be the productivity platform for the future, with next-gen 2D & 3D UX and limitless scalability.
Explore Qt World
Check our Qt demos and case studies in the virtual Qt World
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Näytä tämä julkaisu Instagramissa.
Want to build something for tomorrow, join #QtPeople today! We have loads of cool jobs you don't want to miss! http://qt.io/careers #builtwithQt #software #developers #coding #framework #tool #tooling #C++ #QML #engineers #sales #tech #technology #UI #UX #CX #Qt #Qtdev #global #openpositions #careers #job
Henkilön Qt (@theqtcompany) jakama julkaisu