New classes: QXmlStreamReader and QXmlStreamWriter

Snapshot users have seen them already: with QXmlStreamReader and QXmlStreamWriter, Qt 4.3 will feature two new classes for reading and writing XML.

QXmlStreamReader is a faster and more convenient replacement for Qt's own SAX parser, and in some cases also for applications that would previously use a DOM tree.

The basic concept of a stream reader is to report an XML document as a stream of tokens, similar to SAX. The main difference between QXmlStreamReader and SAX is how these XML tokens are reported. With SAX, the application must provide handlers that receive so-called XML events from the parser at the parser's convenience. With QXmlStreamReader, the application code itself drives the loop and pulls tokens from the reader one after another as it needs them. This is done by calling readNext(), which makes the reader read from the input stream until it has completed a new token, and then returns its tokenType(). A set of convenient functions like isStartElement() or text() then allows to examine this token, and to obtain information about what has been read. The big advantage of the pulling approach is the possibility to build recursive decent parsers, meaning you can split your XML parsing code easily into different methods or classes. This makes it easy to keep track of the application's own state when parsing XML.

The streaming concept is not new, it's what other APIs use as well, e.g. Java's StAX or libxml's TextReader approach. In fact, it's pretty much the straight-forward and intuitive approach to reading tokens. The weird thing was SAX and honestly I can't remember what had driven us to implement SAX in Qt in the first place, other than lack of time to design something better. We had it on the list for a long time, now we finally got around doing it.

The streambookmarks example in the Qt snapshots shows how to use both classes. Here's the typical main loop:

 QXmlStreamReader xml;
while (!xml.atEnd()) {
... // do processing
if (xml.error()) {
... // do error handling

The parser is well-formed and also supports incremental parsing. And it's a lot faster than Qt's SAX and DOM implementations. If things work out well, we might base the SAX parser in future Qt versions on QXmlStreamReader, which would make our SAX implementation both faster and more compliant. But even then, QXmlStreamReader is the fastest and most convenient way to read XML data in Qt.

Blog Topics: