Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
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/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 <tqxml.h> class TQString; class StructureParser : public TQXmlDefaultHandler { public: bool startDocument(); bool startElement( const TQString&, const TQString&, const TQString& , const TQXmlAttributes& ); bool endElement( const TQString&, const TQString&, const TQString& ); private: TQString indent; }; #endif
Implementation:
/**************************************************************************** ** $Id: qt/structureparser.cpp 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. ** *****************************************************************************/ #include "structureparser.h" #include <stdio.h> #include <ntqstring.h> bool StructureParser::startDocument() { indent = ""; return TRUE; } bool StructureParser::startElement( const TQString&, const TQString&, const TQString& qName, const TQXmlAttributes& ) { printf( "%s%s\n", (const char*)indent, (const char*)qName ); indent += " "; return TRUE; } bool StructureParser::endElement( const TQString&, const TQString&, const TQString& ) { indent.remove( (uint)0, 4 ); return TRUE; }
Main:
/**************************************************************************** ** $Id: qt/tagreader.cpp 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. ** *****************************************************************************/ #include "structureparser.h" #include <ntqfile.h> #include <tqxml.h> #include <ntqwindowdefs.h> int main( int argc, char **argv ) { if ( argc < 2 ) { fprintf( stderr, "Usage: %s <xmlfile> [<xmlfile> ...]\n", argv[0] ); return 1; } StructureParser handler; TQXmlSimpleReader reader; reader.setContentHandler( &handler ); for ( int i=1; i < argc; i++ ) { TQFile xmlFile( argv[i] ); TQXmlInputSource source( &xmlFile ); reader.parse( source ); } return 0; }
See also TQt XML Examples.
Copyright © 2007 Trolltech | Trademarks | TQt 3.3.8
|