📄 xml-sax-walkthrough.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - Walkthrough: How to use the Qt SAX2 classes</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Walkthrough: How to use the Qt SAX2 classes</h1><br clear="all">For a general discussion of the XML topics in Qt please refer tothe document <a href="xml.html">Qt XML Module.</a>To learn more about SAX2 see the document describing<a href="xml-sax.html">the Qt SAX2 implementation.</a><p>Before reading on you should at least be familiar with the <a href="xml-sax.html#introSAX2">Introduction to SAX2.</a><p><a name="quickStart"></a><h2>A tiny parser</h2><p>In this section we will present a small example reader that outputsthe names of all elements in an XML document on the command line. The element names are indented corresponding to their nesting level.<p>As mentioned in <a href="xml-sax.html#introSAX2">Introduction to SAX2</a> we have to implement the functions of the handler classes that we areinterested in. In our case these are only three:<a href="qxmlcontenthandler.html#7c53e1">QXmlContentHandler::startDocument()</a>,<a href="qxmlcontenthandler.html#4cb897">QXmlContentHandler::startElement()</a> and<a href="qxmlcontenthandler.html#b9428c">QXmlContentHandler::endElement()</a>.<p>For this purpose we use a subclass of the <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a> (rememberthat the special handler classes are all abstract and the default handler classprovides an implementation that does not change the parsing behavior): <pre>/*$Id: qt/examples/xml/tagreader/structureparser.h 2.3.8 edited 2004-05-12 $*/ #include <<a href="qxml-h.html">qxml.h</a>>class QString;class StructureParser : public QXmlDefaultHandler{public: bool startDocument(); bool startElement( const QString&, const QString&, const QString& , const QXmlAttributes& ); bool endElement( const QString&, const QString&, const QString& );private: <a href="qstring.html">QString</a> indent;};</pre><p>Apart from the private helper variable <em>indent</em> that we will use toget indentation right, there is nothing special about our new <em>StructureParser</em> class. <p>Even the implementation is straight-forward: <pre> #include "structureparser.h" #include <iostream.h> #include <<a href="qstring-h.html">qstring.h</a>></pre><p>First we overload <a href="qxmlcontenthandler.html#4cb897">QXmlContentHandler::startElement()</a> with a non-empty version. <pre> bool StructureParser::startDocument() { indent = ""; return TRUE; }</pre><p>At the beginning of the document we simply set <em>indent</em> to an empty string because wewant to print out the root element without any indentation.Also we return TRUE so that the parser continues without reporting an error.<p>Because we want to be informed when the parser comesaccross a start tag of an element and subsequently print it out, wehave to overload <a href="qxmlcontenthandler.html#4cb897">QXmlContentHandler::startElement()</a>. <pre> bool StructureParser::startElement( const QString&, const QString&, const QString& qName, const QXmlAttributes& ) { cout << indent << qName << endl; indent += " "; return TRUE; }</pre><p>This is what the implementation does: The name of the element withpreceding indentation is printed out followed by a linebreak.Strictly speaking <em>qName</em> contains the local element name without an eventual prefix denoting the<a href="xml.html#namespaces">namespace</a>.<p>If another element follows before the current element's end tagit should be indented. Therefore we add four spaces to the<em>indent</em> string.<p>Finally we return TRUE in order to let the parser continue withouterrors.<p>The last functionality we need to add is the parser's behaviour when an end tag occurs. This means overloading <a href="qxmlcontenthandler.html#b9428c">QXmlContentHandler::endElement()</a>. <pre> bool StructureParser::endElement( const QString&, const QString&, const QString& ) { indent.remove( 0, 4 ); return TRUE; }</pre><p>Obviously we then should shorten the <em>indent</em> string by the fourwhitespaces added in startElement().<p>With this we're done with our parser and can start writing the main() program. <pre> #include "structureparser.h" #include <<a href="qfile-h.html">qfile.h</a>> #include <<a href="qxml-h.html">qxml.h</a>> int main( int argc, char **argv ) { for ( int i=1; i < argc; i++ ) {</pre><p>Successively we deal with all files given as command line arguments. <pre> StructureParser handler;</pre><p>The next step is to create an instance of the <em>StructureParser.</em> <pre> <a href="qfile.html">QFile</a> xmlFile( argv[i] ); <a href="qxmlinputsource.html">QXmlInputSource</a> source( xmlFile );</pre><p>Then we create a<a href="qxmlinputsource.html">QXmlInputSource</a> for the XML file to be parsed. <pre> <a href="qxmlsimplereader.html">QXmlSimpleReader</a> reader; reader.<a href="qxmlsimplereader.html#3ce23b">setContentHandler</a>( &handler );</pre><p>After that we set up the reader. As our <em>StructureParser</em>class deals with <a href="qxmlcontenthandler.html">QXmlContentHandler</a> functionality only we simply register it as the content handler of our choice. <pre> reader.<a href="qxmlsimplereader.html#2d6b2a">parse</a>( source );</pre><p>Now we take our input source and start parsing. <pre> } return 0; }</pre><p>Running the program on the following XML file... <pre><animals><mammals> <monkeys> <gorilla/> <orang-utan/> </monkeys></mammals><birds> <pigeon/> <penguin/> </birds></animals></pre><p>... produces the following output:<pre>animals mammals monkeys gorilla orang-utan birds pigeon penguin</pre><p>It will however refuse to produce the correct result if you e.g. inserta whitespace between a < and the element name in your test-XML file.To prevent such annoyancesyou should always install an error handler with <a href="qxmlreader.html#ea1a35">QXmlReader::setErrorHandler()</a>. This allows you to reportparsing errors to the user.<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -