Parsing XML using Qt

Earlier versions of Qt had a number of XML parsers, each suited to different tasks and different styles of parsing. Fortunately, in Qt 5, this has been streamlined; the key XML parser to use is the QXmlStreamReader class (see http://qt-project.org/doc/qt-5/qxmlstreamreader.html for details). This class reads from a QIODevice subclass and reads XML tags one at a time, letting you switch on the type of tag the parser encounters. Thus, our parser looks something like this:

QXmlStreamReader xml;
xml.setDevice(input);
while (!xml.atEnd()) {
  QXmlStreamReader::TokenType type = xml.readNext();
  switch(type)
  {
    ... // do processing
  }
}
if (xml.hasError()) {
  ... // do error handling
}

The QXMLStreamReader class reads each tag of the XML ...

Get Application Development with Qt Creator - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.