XML Streaming¶
Qt provides two classes for reading and writing XML through a simple streaming API:
QXmlStreamReader
andQXmlStreamWriter
.A stream reader reports an XML document as a stream of tokens. This differs from SAX as SAX applications provide handlers to receive XML events from the parser whereas the
QXmlStreamReader
drives the loop, pulling tokens from the reader when they are needed. This pulling approach makes it possible to build recursive descent parsers, allowing XML parsing code to be split into different methods or classes.
QXmlStreamReader
is a well-formed XML 1.0 parser that excludes external parsed entities. Hence, data provided by the stream reader adheres to the W3C’s criteria for well-formed XML, as long as no error occurs. Otherwise, functions such asatEnd()
,error()
andhasError()
can be used to check and view the errors.An example of
QXmlStreamReader
implementation would be theXbelReader
in QXmlStream Bookmarks Example , which wraps aQXmlStreamReader
. The constructor takestreeWidget
as a parameter and the class has Xbel specific functions:XbelReader(QTreeWidget *treeWidget); ... void readXBEL(); void readTitle(QTreeWidgetItem *item); void readSeparator(QTreeWidgetItem *item); void readFolder(QTreeWidgetItem *item); void readBookmark(QTreeWidgetItem *item); QTreeWidgetItem *createChildItem(QTreeWidgetItem *item); QXmlStreamReader xml; QTreeWidget *treeWidget; ...The
read()
function accepts aQIODevice
and sets it withsetDevice()
. TheraiseError()
function is used to display a custom error message, inidicating that the file’s version is incorrect.bool XbelReader::read(QIODevice *device) { xml.setDevice(device); if (xml.readNextStartElement()) { if (xml.name() == QLatin1String("xbel") && xml.attributes().value(versionAttribute()) == QLatin1String("1.0")) { readXBEL(); } else { xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); } } return !xml.error(); }The pendent to
QXmlStreamReader
isQXmlStreamWriter
, which provides an XML writer with a simple streaming API.QXmlStreamWriter
operates on aQIODevice
and has specialized functions for all XML tokens or events you want to write, such aswriteDTD()
,writeCharacters()
,writeComment()
and so on.To write XML document with
QXmlStreamWriter
, you start a document with thewriteStartDocument()
function and end it withwriteEndDocument()
, which implicitly closes all remaining open tags. Element tags are opened withwriteStartDocument()
and followed bywriteAttribute()
orwriteAttributes()
, element content, and thenwriteEndDocument()
. Also,writeEmptyElement()
can be used to write empty elements.Element content comprises characters, entity references or nested elements. Content can be written with
writeCharacters()
, a function that also takes care of escaping all forbidden characters and character sequences,writeEntityReference()
, or subsequent calls towriteStartElement()
.The
XbelWriter
class from QXmlStream Bookmarks Example wraps aQXmlStreamWriter
. ItswriteFile()
function illustrates the core functions ofQXmlStreamWriter
mentioned above:bool XbelWriter::writeFile(QIODevice *device) { xml.setDevice(device); xml.writeStartDocument(); xml.writeDTD(QStringLiteral("<!DOCTYPE xbel>")); xml.writeStartElement(QStringLiteral("xbel")); xml.writeAttribute(XbelReader::versionAttribute(), QStringLiteral("1.0")); for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) writeItem(treeWidget->topLevelItem(i)); xml.writeEndDocument(); return true; }
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.