Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# 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.

This example is thoroughly explained in a walkthrough.

Header file:

    /*
    $Id: qt/structureparser.h   3.1.2   edited Nov 8 10:35 $
    */

    #ifndef STRUCTUREPARSER_H
    #define STRUCTUREPARSER_H

    #include <qxml.h>
    #include <qptrstack.h>

    class QListView;
    class QListViewItem;
    class QString;

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

        void setListView( QListView * );

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

    #endif

Implementation:

    /*
    $Id: qt/structureparser.cpp   3.1.2   edited Nov 8 10:35 $
    */

    #include "structureparser.h"

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

    StructureParser::StructureParser( QListView * t )
                    : QXmlDefaultHandler()
    {
        setListView( t );
    }

    void StructureParser::setListView( QListView * t )
    {
        table = t;
        table->setSorting( -1 );
        table->addColumn( "Qualified name" );
        table->addColumn( "Namespace" );
    }

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

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

        if ( attributes.length() > 0 ) {
            for ( int i = 0 ; i < attributes.length(); i++ ) {
                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/tagreader.cpp   3.1.2   edited Nov 8 10:35 $
    **
    ** Copyright (C) 2001 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #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 * handler = new StructureParser( nameSpace );
        reader.setContentHandler( handler );
        reader.parse( source );

        QListView * namespacePrefix = new QListView( container,
                                                     "table_namespace_prefix" );
        handler->setListView( namespacePrefix );
        reader.setFeature( "http://xml.org/sax/features/namespace-prefixes",
                           TRUE );
        source.reset();
        reader.parse( source );

        QListView * prefix = new QListView( container, "table_prefix");
        handler->setListView( prefix );
        reader.setFeature( "http://xml.org/sax/features/namespaces", FALSE );
        source.reset();
        reader.parse( source );

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

        // namespace prefix label
        (void) new QLabel(
                 "\n"
                 "http://xml.org/sax/features/namespaces: TRUE\n"
                 "http://xml.org/sax/features/namespace-prefixes: TRUE\n",
                 container );

        // prefix label
        (void) 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->show();
        return app.exec();
    }

See also Qt XML Examples.