221 lines
9.5 KiB
221 lines
9.5 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 TQt 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 Classes</font></a>
|
|
| <a href="mainclasses.html">
|
|
<font color="#004faf">Main Classes</font></a>
|
|
| <a href="annotated.html">
|
|
<font color="#004faf">Annotated</font></a>
|
|
| <a href="groups.html">
|
|
<font color="#004faf">Grouped 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 TQt SAX2 classes</h1>
|
|
|
|
|
|
<p>
|
|
<p> For a general discussion of the XML topics in TQt 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 TQt 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="tqxmlcontenthandler.html#startDocument">TQXmlContentHandler::startDocument</a>(),
|
|
<a href="tqxmlcontenthandler.html#startElement">TQXmlContentHandler::startElement</a>() and
|
|
<a href="tqxmlcontenthandler.html#endElement">TQXmlContentHandler::endElement</a>().
|
|
<p> For this purpose we use a subclass of the <a href="tqxmldefaulthandler.html">TQXmlDefaultHandler</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 TQt. This example
|
|
** program may be used, distributed and modified without limitation.
|
|
**
|
|
*****************************************************************************/
|
|
|
|
#ifndef STRUCTUREPARSER_H
|
|
#define STRUCTUREPARSER_H
|
|
|
|
#include <<a href="tqxml-h.html">tqxml.h</a>>
|
|
|
|
class TQString;
|
|
|
|
class StructureParser : public <a href="tqxmldefaulthandler.html">TQXmlDefaultHandler</a>
|
|
{
|
|
public:
|
|
bool startDocument();
|
|
bool startElement( const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>& ,
|
|
const <a href="tqxmlattributes.html">TQXmlAttributes</a>& );
|
|
bool endElement( const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>& );
|
|
|
|
private:
|
|
<a href="tqstring.html">TQString</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 <stdio.h>
|
|
#include <<a href="tqstring-h.html">tqstring.h</a>>
|
|
</pre>
|
|
<p> First we overload <a href="tqxmlcontenthandler.html#startDocument">TQXmlContentHandler::startDocument</a>() with a non-empty version.
|
|
<p> <pre> <a name="x2137"></a>bool StructureParser::<a href="tqxmlcontenthandler.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="tqxmlcontenthandler.html#startElement">TQXmlContentHandler::startElement</a>().
|
|
<p> <pre> <a name="x2138"></a>bool StructureParser::<a href="tqxmlcontenthandler.html#startElement">startElement</a>( const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>&,
|
|
const <a href="tqstring.html">TQString</a>& qName,
|
|
const <a href="tqxmlattributes.html">TQXmlAttributes</a>& )
|
|
{
|
|
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="tqxmlcontenthandler.html#endElement">TQXmlContentHandler::endElement</a>().
|
|
<p> <pre> <a name="x2136"></a>bool StructureParser::<a href="tqxmlcontenthandler.html#endElement">endElement</a>( const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>&, const <a href="tqstring.html">TQString</a>& )
|
|
{
|
|
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 <<a href="qfile-h.html">ntqfile.h</a>>
|
|
#include <<a href="tqxml-h.html">tqxml.h</a>>
|
|
#include <<a href="qwindowdefs-h.html">ntqwindowdefs.h</a>>
|
|
|
|
int main( int argc, char **argv )
|
|
{
|
|
if ( argc < 2 ) {
|
|
fprintf( stderr, "Usage: %s <xmlfile> [<xmlfile> ...]\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="tqxmlsimplereader.html">TQXmlSimpleReader</a> reader;
|
|
<a name="x2140"></a> reader.<a href="tqxmlreader.html#setContentHandler">setContentHandler</a>( &handler );
|
|
</pre>
|
|
<p> After that we set up the reader. As our <em>StructureParser</em>
|
|
class deals with <a href="tqxmlcontenthandler.html">TQXmlContentHandler</a> functionality only
|
|
we simply register it as the content handler of our choice.
|
|
<p> <pre> for ( int i=1; i < argc; i++ ) {
|
|
</pre>
|
|
<p> Successively we deal with all files given as command line arguments.
|
|
<p> <pre> <a href="ntqfile.html">TQFile</a> xmlFile( argv[i] );
|
|
<a href="tqxmlinputsource.html">TQXmlInputSource</a> source( &xmlFile );
|
|
</pre>
|
|
<p> Then we create a
|
|
<a href="tqxmlinputsource.html">TQXmlInputSource</a> for the XML file to be parsed.
|
|
<p> <pre> <a name="x2139"></a> reader.<a href="tqxmlsimplereader.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><animals>
|
|
<mammals>
|
|
<monkeys> <gorilla/> <orangutan/> </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. insert
|
|
a whitespace between a < and the element name in your test-XML file.
|
|
To prevent such annoyances
|
|
you should always install an error handler with <a href="tqxmlreader.html#setErrorHandler">TQXmlReader::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 © 2007
|
|
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
|
<td align=right><div align=right>TQt 3.3.8</div>
|
|
</table></div></address></body>
|
|
</html>
|