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.3.8   edited May 27 2003 $
*/

#ifndef STRUCTUREPARSER_H
#define STRUCTUREPARSER_H

#include <ntqxml.h>
#include <ntqptrstack.h>

class TQListView;
class TQListViewItem;
class TQString;

class StructureParser: public TQXmlDefaultHandler
{
public:
    StructureParser( TQListView * );
    bool startElement( const TQString&, const TQString&, const TQString& ,
                       const TQXmlAttributes& );
    bool endElement( const TQString&, const TQString&, const TQString& );

    void setListView( TQListView * );

private:
    TQPtrStack<TQListViewItem> stack;
    TQListView * table;
};

#endif


Implementation:

/*
$Id: qt/structureparser.cpp   3.3.8   edited May 27 2003 $
*/

#include "structureparser.h"

#include <ntqstring.h>
#include <ntqlistview.h>

StructureParser::StructureParser( TQListView * t )
                : TQXmlDefaultHandler()
{
    setListView( t );
}

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

bool StructureParser::startElement( const TQString& namespaceURI,
                                    const TQString& ,
                                    const TQString& qName,
                                    const TQXmlAttributes& attributes)
{
    TQListViewItem * element;

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

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

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


Main:

/****************************************************************************
** $Id: qt/tagreader.cpp   3.3.8   edited Jan 11 14:46 $
**
** Copyright (C) 2005-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 <ntqapplication.h>
#include <ntqfile.h>
#include <ntqxml.h>
#include <ntqlistview.h>
#include <ntqgrid.h>
#include <ntqmainwindow.h>
#include <ntqlabel.h>

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

    TQFile xmlFile( argc == 2 ? argv[1] : "fnord.xml" );
    TQXmlInputSource source( &xmlFile );

    TQXmlSimpleReader reader;

    TQGrid * container = new TQGrid( 3 );

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

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

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

    // namespace label
    (void) new TQLabel(
             "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 TQLabel(
             "\n"
             "http://xml.org/sax/features/namespaces: TRUE\n"
             "http://xml.org/sax/features/namespace-prefixes: TRUE\n",
             container );

    // prefix label
    (void) new TQLabel(
             "\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 TQt XML Examples.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8