[QT Tutorial] 30th XML (iv) using stream to read and write XML

Source: Internet
Author: User
[QT Tutorial] 30th XML (iv) using stream to read and write XMLLandlord posted on 2013-5-22 13:03:33 | Views: 611 | Replies: 0
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))      {&nbsp ;       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/");     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.