Back to Blog home

Parsing XML with Qt: Updates for Qt 6

Published on Tuesday December 03, 2019 by Sona Kurazyan in Dev Loop XML | Comments

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 QDomDocument:

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 Blog

Stay up to date with the latest marketing, sales and service tips and news.