Book Image

Application Development with Qt Creator - Second Edition

Book Image

Application Development with Qt Creator - Second Edition

Overview of this book

Table of Contents (20 chapters)
Application Development with Qt Creator Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 in turn, each time its readNext method is called. For each tag read, readNext returns the type of the tag read, which will be one of the following:

  • StartDocument: This indicates the beginning of the document

  • EndDocument: This indicates...