Using streams to read and write XML Copyright notice this article original in the author Yafeilinux, reproduced please indicate the source.
Introductory lead introduced two new classes from QT 4.3 to read and write XML documents: Qxmlstreamreader and Qxmlstreamwriter. The Qxmlstreamreader class provides a fast parser to read well-formed XML documents through a simple streaming API, which appears as an alternative to the SAX parser for QT because it is faster and more convenient than a SAX parser. Qxmlstreamreader can read data from Qiodevice or Qbytearray. The basic principle of a stream reader is to report an XML document as a tick (tokens) stream, which is similar to sax, which differs in how the XML notation is reported. In sax, an application must provide a processor (callback function) to obtain so-called XML events from the parser, and for Qxmlstreamreader, it is the application code itself that drives the loop, which can be pulled one after another from the reader when needed. This is done by calling the Readnext () function, which reads the next tick, then returns a tick type, and then uses functions such as isstartelement () and text () to determine whether the token contains the information we need. The biggest benefit of using this method of active pull notation is the ability to construct recursive parsers, which can be used to manipulate different tokens in XML documents in different functions or classes.
Environment: Windows Xp + Qt 4.8.4+qt Creator2.6.2
Directory one, parsing XML document II, writing XML documents
Body I. Parsing an XML document 1. Create a new QT console app with the project name Myxmlstream, and then change the first line of the Myxmlstream.pro file to: QT + + core XML and save the file. 2. Then open the Main.cpp file and change the content as follows: #include <QCoreApplication> #include <QFile> #include <QXmlStreamReader> #include <QXmlStreamWriter> #include <QDebug> int main (int argc, char *argv[]) { Qcoreapplication A (argc, argv); QFile file (".. /myxmlstream/my.xml "); if (!file.open (qfile::readonly | Qfile::text)) { Qdebug () << "Error:cannot open File"; return 1; }
Qxmlstreamreader reader;
Setting the file, the stream will be set to the initial state Reader.setdevice (&file);
If the end of the document is not read and no error occurs while (!reader.atend ()) { Reads the next token, which returns the type of the token Qxmlstreamreader::tokentype type = Reader.readnext ();
The following are different outputs depending on the type of token if (type = = qxmlstreamreader::startdocument) Qdebug () << reader.documentencoding () << reader.documentversion (); if (type = = Qxmlstreamreader::startelement) { Qdebug () << "<" << Reader.name () << ">"; if (Reader.attributes (). Hasattribute ("id")) Qdebug () << reader.attributes (). Value ("id"); } if (type = = qxmlstreamreader::endelement) Qdebug () << "</" << reader.name () << ">"; if (type = = Qxmlstreamreader::characters &&!reader.iswhitespace ()) Qdebug () << reader.text (); }
If an error occurs during the read, the output error message if (Reader.haserror ()) { Qdebug () << "error:" << reader.errorstring (); }
File.close ();
return A.exec (); } The copy code can see that the stream reader is constantly reading tokens in a loop by using Readnext (), where different tokens and different content can be processed differently, either in this function or in other functions or other classes. You can copy the previously generated my.xml file to the source directory, and then run the program to see the effect.
Second, write XML document
Corresponding to the Qxmlstreamreader is Qxmlstreamwriter, which provides an XML writer through a simple streaming API. The use of Qxmlstreamwriter is very simple, only need to call the corresponding notation of the write function to write the relevant data. Change the contents of the preceding main function to: int Main (int argc, char *argv[]) { qcoreapplication A (argc, argv); QFile file (".. /myxmlstream/my2.xml "); if (!file.open (QFile:: WriteOnly | QFile:: Text)) {  ; qdebug () << "Error:cannot open File"; return 1; qxmlstreamwriter Stream (&file); stream.setautoformatting (true); stream.writestartdocument (); stream.writestartelement ("bookmark"); stream.writeattribute ("href", "http://qt.nokia.com/"); |