You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
qt3/doc/html/xml-sax-walkthrough.html

221 lines
9.4 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/xml-sax-walkthrough.doc:36 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Walkthrough: How to use the Qt SAX2 classes</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Walkthrough: How to use the Qt SAX2 classes</h1>
<p>
<p> For a general discussion of the XML topics in Qt please refer to
the document <a href="xml.html">XML Module.</a>
To learn more about SAX2 see the document describing
<a href="xml.html#sax2">the Qt SAX2 implementation.</a>
<p> Before reading on you should at least be familiar with
the <a href="xml.html#sax2Intro">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 outputs
the 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.html#sax2Intro">Introduction to SAX2</a>
we have to implement the functions of the handler classes that we are
interested in. In our case these are only three:
<a href="qxmlcontenthandler.html#startDocument">QXmlContentHandler::startDocument</a>(),
<a href="qxmlcontenthandler.html#startElement">QXmlContentHandler::startElement</a>() and
<a href="qxmlcontenthandler.html#endElement">QXmlContentHandler::endElement</a>().
<p> For this purpose we use a subclass of the <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a> (remember
that the special handler classes are all abstract and the default handler class
provides an implementation that does not change the parsing behavior):
<p> <pre>/****************************************************************************
** $Id: qt/structureparser.h 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef STRUCTUREPARSER_H
#define STRUCTUREPARSER_H
#include &lt;<a href="qxml-h.html">qxml.h</a>&gt;
class QString;
class StructureParser : public <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a>
{
public:
bool startDocument();
bool startElement( const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp; ,
const <a href="qxmlattributes.html">QXmlAttributes</a>&amp; );
bool endElement( const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp; );
private:
<a href="qstring.html">QString</a> indent;
};
#endif
</pre>
<p> Apart from the private helper variable <em>indent</em> that we will use to
get indentation right, there is nothing special about our new
<em>StructureParser</em> class.
<p>
<p> Even the implementation is straight-forward:
<p> <pre> #include "structureparser.h"
#include &lt;stdio.h&gt;
#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
</pre>
<p> First we overload <a href="qxmlcontenthandler.html#startDocument">QXmlContentHandler::startDocument</a>() with a non-empty version.
<p> <pre> <a name="x2137"></a>bool StructureParser::<a href="qxmlcontenthandler.html#startDocument">startDocument</a>()
{
indent = "";
return TRUE;
}
</pre>
<p> At the beginning of the document we simply
set <em>indent</em> to an empty string because we
want 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 comes
accross a start tag of an element and subsequently print it out, we
have to overload <a href="qxmlcontenthandler.html#startElement">QXmlContentHandler::startElement</a>().
<p> <pre> <a name="x2138"></a>bool StructureParser::<a href="qxmlcontenthandler.html#startElement">startElement</a>( const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp;,
const <a href="qstring.html">QString</a>&amp; qName,
const <a href="qxmlattributes.html">QXmlAttributes</a>&amp; )
{
printf( "%s%s\n", (const char*)indent, (const char*)qName );
indent += " ";
return TRUE;
}
</pre>
<p> This is what the implementation does: The name of the element with
preceding 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 tag
it 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 without
errors.
<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#endElement">QXmlContentHandler::endElement</a>().
<p> <pre> <a name="x2136"></a>bool StructureParser::<a href="qxmlcontenthandler.html#endElement">endElement</a>( const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp;, const <a href="qstring.html">QString</a>&amp; )
{
indent.remove( (uint)0, 4 );
return TRUE;
}
</pre>
<p> Obviously we then should shorten the <em>indent</em> string by the four
whitespaces added in startElement().
<p> With this we're done with our parser and can start writing the main()
program.
<p>
<p> <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;
#include &lt;<a href="qwindowdefs-h.html">qwindowdefs.h</a>&gt;
int main( int argc, char **argv )
{
if ( argc &lt; 2 ) {
fprintf( stderr, "Usage: %s &lt;xmlfile&gt; [&lt;xmlfile&gt; ...]\n", argv[0] );
return 1;
}
</pre>
<p> This check ensures that we have a sequence of files from the command
line to examine.
<p> <pre> StructureParser handler;
</pre>
<p> The next step is to create an instance of the <em>StructureParser</em>.
<p> <pre> <a href="qxmlsimplereader.html">QXmlSimpleReader</a> reader;
<a name="x2140"></a> reader.<a href="qxmlreader.html#setContentHandler">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.
<p> <pre> for ( int i=1; i &lt; argc; i++ ) {
</pre>
<p> Successively we deal with all files given as command line arguments.
<p> <pre> <a href="qfile.html">QFile</a> xmlFile( argv[i] );
<a href="qxmlinputsource.html">QXmlInputSource</a> source( &amp;xmlFile );
</pre>
<p> Then we create a
<a href="qxmlinputsource.html">QXmlInputSource</a> for the XML file to be parsed.
<p> <pre> <a name="x2139"></a> reader.<a href="qxmlsimplereader.html#parse">parse</a>( source );
</pre>
<p> Now we take our input source and start parsing.
<p> <pre> }
return 0;
}
</pre>
<p> Running the program on the following XML file...
<p> <pre>&lt;animals&gt;
&lt;mammals&gt;
&lt;monkeys&gt; &lt;gorilla/&gt; &lt;orangutan/&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. insert
a whitespace between a &lt; and the element name in your test-XML file.
To prevent such annoyances
you should always install an error handler with <a href="qxmlreader.html#setErrorHandler">QXmlReader::setErrorHandler</a>(). This allows you to report
parsing errors to the user.
<p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>.
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>