Sax Mode
Using the Sax method to parse an XML document is much clearer than using the DOM method, and more importantly, it is much more efficient, but the Sax method is only suitable for reading XML documents.
First , contains the basic header file
#include <QXmlDefaultHandler>
second, the basic Operation
1. Read the XML file
Declares a class myXML inherits the Qxmldefaulthandler class, overriding
Four functions of Startelement (), EndElement (), characters () and FatalError ();
The start tag of an element has been parsed
BOOL Mysax::startelement (const QString &namespaceuri,
Const QString &localname,
Const QString &qname,
Const Qxmlattributes &atts)
Finished parsing a piece of character data
BOOL Mysax::characters (const QString &ch)
Finished parsing an element's end tag
BOOL Mysax::endelement (const QString &namespaceuri,
Const QString &localname,
Const QString &qname)
Error handling
BOOL Mysax::fatalerror (const qxmlparseexception &exception)
Defines the myXML object Myxml.readxml ();
ReadXml ()
{
Read File contents
Qxmlinputsource InputSource (&file);
Creating Qxmlsimplereader Objects
Qxmlsimplereader reader;
Setting up the Content processor
Reader.setcontenthandler (this);
Setting the error handler
Reader.seterrorhandler (this);
Parsing files
Return Reader.parse (InputSource);
}
To read an XML file by using a streaming method
starting with Qt 4.3, two new classes were introduced to read and write XML documents: Qxmlstreamreader and Qxmlstreamwriter. Qxmlstreamreader class
Provides a quick parser to read well-formed XML documents through a simple streaming API, which is present as a substitute for the SAX parser of QT, because
It is faster and more convenient than the 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, similar to sax, which differs in that the XML notation is reported
The way it was told. In sax, an application must provide a processor (callback function) to obtain so-called XML events from the parser, and for Qxmlstreamreader, it should be
Use the program code itself to drive the loop, and you can pull the token from the reader one by one when needed. This is done by calling the Readnext () function, which can
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.
First , include the header file
#include <QFile>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
second, the basic Operation
1. Writing Files
QFile file ("My.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/");
Stream.writetextelement ("title", "Qt Home");
Stream.writeendelement ();
Stream.writeenddocument ();
File.close ();
Qdebug () << "Write finished!";
Here, the setautoformatting (true) function is used to automatically format the formatting, which wraps the line and adds the indentation.
Then using WriteStartDocument (), the function automatically adds the first line of the XML description
(i.e. <?xmlversion= "1.0" encoding= "UTF-8"?>), adding elements can use WriteStartElement (),
However, it is important to note that you must use Writetextelement () to close the previous open element after adding the element's attributes, text, and so on.
Use WriteEndDocument () to finish writing the document at the end.
2. Read the file
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 ();
You can see that the stream reader is constantly reading tokens in a loop by using Readnext (),
Different tokens and different content can be handled differently, either in this function or in other functions or other classes.