| Classes - Annotated - Tree - Functions - Home - Structure

# Demonstration of SAX2 features

This example presents a small SAX2 reader that outputs the qualified names and the respective namespace URIs of all elements and attributes in an XML file. Additionally the tree structure of the document is displayed. In three listviews the program shows the different output of the reader depending on how the SAX2 features http://xml.org/sax/features/namespaces and http://xml.org/sax/features/namespace-prefixes are set.

Header file:

    /*
    $Id: qt/examples/xml/tagreader-with-features/structureparser.h   2.3.5   edited 2001-01-26 $
    */

    #include <qxml.h>
    #include <qstack.h>

    class QListView;
    class QListViewItem;
    class QString;

    class StructureParser: public QXmlDefaultHandler
    {
    public:
        StructureParser( QListView * );
        bool startDocument();
        bool startElement( const QString&, const QString&, const QString& ,
                           const QXmlAttributes& );
        bool endElement( const QString&, const QString&, const QString& );

    private:
        QStack<QListViewItem> stack;
        QListView * table;
    };

Implementation:

    /*
    $Id: qt/examples/xml/tagreader-with-features/structureparser.cpp   2.3.5   edited 2001-01-26 $
    */

    #include "structureparser.h"

    #include <qstring.h>
    #include <qlistview.h>

    StructureParser::StructureParser( QListView * t )
                    : QXmlDefaultHandler()
    {
        table = t;
        table->setSorting( -1 ); // no sorting
        table->addColumn( "Qualified name" );
        table->addColumn( "Namespace" );
    }

    bool StructureParser::startDocument()
    {
        return TRUE;
    }

    bool StructureParser::startElement( const QString& namespaceURI, const QString& ,
                                        const QString& qName,
                                        const QXmlAttributes& attributes)
    {
        QListViewItem * element;

        if ( ! stack.isEmpty() ){
            element = new QListViewItem( stack.top(), qName, namespaceURI );
        } else {
            element = new QListViewItem( table, qName, namespaceURI );
        }
        stack.push( element );
        element->setOpen( TRUE );

        if ( attributes.length() > 0 ){
            QListViewItem * attribute;
            for ( int i = 0 ; i < attributes.length(); i++ ){
                attribute = new QListViewItem( element,
                                               attributes.qName(i),
                                               attributes.uri(i) );
            }
        }
        return TRUE;
    }

    bool StructureParser::endElement( const QString&, const QString&, const QString& )
    {
        stack.pop();
        return TRUE;
    }

Main:

    /*
    $Id: qt/examples/xml/tagreader-with-features/tagreader.cpp   2.3.5   edited 2001-06-12 $
    */

    #include "structureparser.h"
    #include <qapplication.h>
    #include <qfile.h>
    #include <qxml.h>
    #include <qlistview.h>
    #include <qgrid.h>
    #include <qmainwindow.h>
    #include <qlabel.h>

    int main( int argc, char **argv )
    {
        QApplication app( argc, argv );

        QFile xmlFile( argc == 2 ? argv[1] : "fnord.xml" );
        QXmlInputSource source( xmlFile );

        QXmlSimpleReader reader;

        QGrid * container = new QGrid( 3 );

        QListView * nameSpace = new QListView( container, "table_namespace" );
        StructureParser * handlerNamespace = new StructureParser( nameSpace );
        reader.setContentHandler( handlerNamespace );
        reader.parse( source );

        QListView * namespacePrefix = new QListView( container, "table_namespace_prefix" );
        StructureParser * handlerNamespacePrefix = new StructureParser( namespacePrefix );
        reader.setContentHandler( handlerNamespacePrefix );
        reader.setFeature( "http://xml.org/sax/features/namespaces", TRUE );
        reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", TRUE );
        reader.parse( source );

        QListView * prefix = new QListView( container, "table_prefix");
        StructureParser * handlerPrefix = new StructureParser( prefix );
        reader.setContentHandler( handlerPrefix );
        reader.setFeature( "http://xml.org/sax/features/namespaces", FALSE );
        reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", TRUE );
        reader.parse( source );

        QLabel * namespaceLabel = new QLabel(
                                 "Default:\n"
                                 "http://xml.org/sax/features/namespaces: TRUE\n"
                                 "http://xml.org/sax/features/namespace-prefixes: FALSE\n",
                                 container );

        QLabel * namespacePrefixLabel = new QLabel(
                                 "\n"
                                 "http://xml.org/sax/features/namespaces: TRUE\n"
                                 "http://xml.org/sax/features/namespace-prefixes: TRUE\n",
                                 container );

        QLabel * prefixLabel = new QLabel(
                                 "\n"
                                 "http://xml.org/sax/features/namespaces: FALSE\n"
                                 "http://xml.org/sax/features/namespace-prefixes: TRUE\n",
                                 container );

        app.setMainWidget( container );
        container->setCaption("Qt Example - XML Tagreader");
        container->show();
        return app.exec();
    }