⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml-sax-walkthrough.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 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>/*&#36;Id&#58; qt/examples/xml/tagreader/structureparser.h   2.3.8   edited 2004-05-12 $*/  #include &lt;<a href="qxml-h.html">qxml.h</a>&gt;class QString;class StructureParser : public QXmlDefaultHandler{public:    bool startDocument();    bool startElement( const QString&amp;, const QString&amp;, const QString&amp; ,                        const QXmlAttributes&amp; );    bool endElement( const QString&amp;, const QString&amp;, const QString&amp; );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 &lt;iostream.h&gt;    #include &lt;<a href="qstring-h.html">qstring.h</a>&gt;</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&amp;, const QString&amp;,                                         const QString&amp; qName,                                         const QXmlAttributes&amp; )    {        cout &lt;&lt; indent &lt;&lt; qName &lt;&lt; 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&amp;, const QString&amp;, const QString&amp; )    {        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 &lt;<a href="qfile-h.html">qfile.h</a>&gt;    #include &lt;<a href="qxml-h.html">qxml.h</a>&gt;    int main( int argc, char **argv )    {        for ( int i=1; i &lt; 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>( &amp;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>&lt;animals&gt;&lt;mammals&gt;  &lt;monkeys&gt; &lt;gorilla/&gt; &lt;orang-utan/&gt; &lt;/monkeys&gt;&lt;/mammals&gt;&lt;birds&gt; &lt;pigeon/&gt; &lt;penguin/&gt; &lt;/birds&gt;&lt;/animals&gt;</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 &lt; 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 + -