O'Reilly logo

Application Development with Qt Creator - Second Edition by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required