A tiny SAX2 parser
This example presents a small SAX2
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
This example is thoroughly explained in a
walkthrough.
Header file: /*
$Id: qt/examples/xml/tagreader/structureparser.h 2.3.1 edited 2001-01-26 $
*/
#include <qxml.h>
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:
QString indent;
};
Implementation: /*
$Id: qt/examples/xml/tagreader/structureparser.cpp 2.3.1 edited 2001-01-26 $
*/
#include "structureparser.h"
#include <iostream.h>
#include <qstring.h>
bool StructureParser::startDocument()
{
indent = "";
return TRUE;
}
bool StructureParser::startElement( const QString&, const QString&,
const QString& qName,
const QXmlAttributes& )
{
cout << indent << qName << endl;
indent += " ";
return TRUE;
}
bool StructureParser::endElement( const QString&, const QString&, const QString& )
{
indent.remove( 0, 4 );
return TRUE;
}
Main:
/*
$Id: qt/examples/xml/tagreader/tagreader.cpp 2.3.1 edited 2001-01-26 $
*/
#include "structureparser.h"
#include <qfile.h>
#include <qxml.h>
int main( int argc, char **argv )
{
for ( int i=1; i < argc; i++ ) {
StructureParser handler;
QFile xmlFile( argv[i] );
QXmlInputSource source( xmlFile );
QXmlSimpleReader reader;
reader.setContentHandler( &handler );
reader.parse( source );
}
return 0;
}
Copyright © 2000 Trolltech | Trademarks
| Qt version 2.3.1
|