diff options
Diffstat (limited to 'examples/xml')
-rw-r--r-- | examples/xml/outliner/main.cpp | 22 | ||||
-rw-r--r-- | examples/xml/outliner/outliner.doc | 40 | ||||
-rw-r--r-- | examples/xml/outliner/outliner.pro | 11 | ||||
-rw-r--r-- | examples/xml/outliner/outlinetree.cpp | 103 | ||||
-rw-r--r-- | examples/xml/outliner/outlinetree.h | 30 | ||||
-rw-r--r-- | examples/xml/outliner/todos.opml | 55 | ||||
-rw-r--r-- | examples/xml/tagreader-with-features/fnord.xml | 13 | ||||
-rw-r--r-- | examples/xml/tagreader-with-features/structureparser.cpp | 56 | ||||
-rw-r--r-- | examples/xml/tagreader-with-features/structureparser.h | 29 | ||||
-rw-r--r-- | examples/xml/tagreader-with-features/tagreader-with-features.pro | 11 | ||||
-rw-r--r-- | examples/xml/tagreader-with-features/tagreader.cpp | 74 | ||||
-rw-r--r-- | examples/xml/tagreader-with-features/tagreader.doc | 40 | ||||
-rw-r--r-- | examples/xml/tagreader/animals.xml | 7 | ||||
-rw-r--r-- | examples/xml/tagreader/structureparser.cpp | 34 | ||||
-rw-r--r-- | examples/xml/tagreader/structureparser.h | 29 | ||||
-rw-r--r-- | examples/xml/tagreader/tagreader.cpp | 30 | ||||
-rw-r--r-- | examples/xml/tagreader/tagreader.doc | 35 | ||||
-rw-r--r-- | examples/xml/tagreader/tagreader.pro | 11 |
18 files changed, 630 insertions, 0 deletions
diff --git a/examples/xml/outliner/main.cpp b/examples/xml/outliner/main.cpp new file mode 100644 index 000000000..c4b1491e3 --- /dev/null +++ b/examples/xml/outliner/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 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 <qapplication.h> +#include "outlinetree.h" + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + OutlineTree outline( "todos.opml" ); + a.setMainWidget( &outline ); + outline.show(); + + return a.exec(); +} diff --git a/examples/xml/outliner/outliner.doc b/examples/xml/outliner/outliner.doc new file mode 100644 index 000000000..c21043dd6 --- /dev/null +++ b/examples/xml/outliner/outliner.doc @@ -0,0 +1,40 @@ +/* +*/ + +/*! \page outliner-example.html + + \ingroup xml-examples + + \title Outliner to show use of DOM + + This example presents a small outliner program to show the basic usage of + the \link xml.html#dom DOM classes \endlink. The format of the outlines + is the OPML format as described in http://www.opml.org/spec. + + This example shows how to load a DOM tree from an XML file and how to + traverse it. + + <hr> + + Sample XML file (todos.opml): + + \include xml/outliner/todos.opml + + <hr> + + Header file (outlinetree.h): + + \include xml/outliner/outlinetree.h + + <hr> + + Implementation (outlinetree.cpp): + + \include xml/outliner/outlinetree.cpp + + <hr> + + Main (main.cpp): + + \include xml/outliner/main.cpp +*/ diff --git a/examples/xml/outliner/outliner.pro b/examples/xml/outliner/outliner.pro new file mode 100644 index 000000000..1dcdc44bd --- /dev/null +++ b/examples/xml/outliner/outliner.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = outliner + +CONFIG += qt warn_on release + +REQUIRES = xml large-config + +HEADERS = outlinetree.h +SOURCES = main.cpp \ + outlinetree.cpp +INTERFACES = diff --git a/examples/xml/outliner/outlinetree.cpp b/examples/xml/outliner/outlinetree.cpp new file mode 100644 index 000000000..69b92358b --- /dev/null +++ b/examples/xml/outliner/outlinetree.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 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 "outlinetree.h" +#include <qfile.h> +#include <qmessagebox.h> + +OutlineTree::OutlineTree( const TQString fileName, TQWidget *parent, const char *name ) + : TQListView( parent, name ) +{ + // div. configuration of the list view + addColumn( "Outlines" ); + setSorting( -1 ); + setRootIsDecorated( TRUE ); + + // read the XML file and create DOM tree + TQFile opmlFile( fileName ); + if ( !opmlFile.open( IO_ReadOnly ) ) { + TQMessageBox::critical( 0, + tr( "Critical Error" ), + tr( "Cannot open file %1" ).arg( fileName ) ); + return; + } + if ( !domTree.setContent( &opmlFile ) ) { + TQMessageBox::critical( 0, + tr( "Critical Error" ), + tr( "Parsing error for file %1" ).arg( fileName ) ); + opmlFile.close(); + return; + } + opmlFile.close(); + + // get the header information from the DOM + TQDomElement root = domTree.documentElement(); + TQDomNode node; + node = root.firstChild(); + while ( !node.isNull() ) { + if ( node.isElement() && node.nodeName() == "head" ) { + TQDomElement header = node.toElement(); + getHeaderInformation( header ); + break; + } + node = node.nextSibling(); + } + // create the tree view out of the DOM + node = root.firstChild(); + while ( !node.isNull() ) { + if ( node.isElement() && node.nodeName() == "body" ) { + TQDomElement body = node.toElement(); + buildTree( 0, body ); + break; + } + node = node.nextSibling(); + } +} + +OutlineTree::~OutlineTree() +{ +} + +void OutlineTree::getHeaderInformation( const TQDomElement &header ) +{ + // visit all children of the header element and look if you can make + // something with it + TQDomNode node = header.firstChild(); + while ( !node.isNull() ) { + if ( node.isElement() ) { + // case for the different header entries + if ( node.nodeName() == "title" ) { + TQDomText textChild = node.firstChild().toText(); + if ( !textChild.isNull() ) { + setColumnText( 0, textChild.nodeValue() ); + } + } + } + node = node.nextSibling(); + } +} + +void OutlineTree::buildTree( TQListViewItem *parentItem, const TQDomElement &parentElement ) +{ + TQListViewItem *thisItem = 0; + TQDomNode node = parentElement.firstChild(); + while ( !node.isNull() ) { + if ( node.isElement() && node.nodeName() == "outline" ) { + // add a new list view item for the outline + if ( parentItem == 0 ) + thisItem = new TQListViewItem( this, thisItem ); + else + thisItem = new TQListViewItem( parentItem, thisItem ); + thisItem->setText( 0, node.toElement().attribute( "text" ) ); + // recursive build of the tree + buildTree( thisItem, node.toElement() ); + } + node = node.nextSibling(); + } +} diff --git a/examples/xml/outliner/outlinetree.h b/examples/xml/outliner/outlinetree.h new file mode 100644 index 000000000..c6a22ad9d --- /dev/null +++ b/examples/xml/outliner/outlinetree.h @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 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 OUTLINETREE_H +#define OUTLINETREE_H + +#include <qlistview.h> +#include <qdom.h> + +class OutlineTree : public TQListView +{ + Q_OBJECT + +public: + OutlineTree( const TQString fileName, TQWidget *parent = 0, const char *name = 0 ); + ~OutlineTree(); + +private: + TQDomDocument domTree; + void getHeaderInformation( const TQDomElement &header ); + void buildTree( TQListViewItem *parentItem, const TQDomElement &parentElement ); +}; + +#endif // OUTLINETREE_H diff --git a/examples/xml/outliner/todos.opml b/examples/xml/outliner/todos.opml new file mode 100644 index 000000000..62793900a --- /dev/null +++ b/examples/xml/outliner/todos.opml @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<opml version="1.0"> + <head> + <title>Todo List</title> + <dateCreated>Tue, 31 Oct 2000 17:00:17 CET</dateCreated> + <dateModified>Tue, 31 Oct 2000 17:00:17 CET</dateModified> + <ownerName>Arthur Dent</ownerName> + <ownerEmail>info@trolltech.com</ownerEmail> + </head> + <body> + <outline text="Background"> + <outline text="This is an example todo list."/> + </outline> + <outline text="Books to read"> + <outline text="Science Fiction"> + <outline text="Philip K. Dick"> + <outline text="Do Androids Dream of Electical Sheep?"/> + <outline text="The Three Stigmata of Palmer Eldritch"/> + </outline> + <outline text="Robert A. Heinlein"> + <outline text="Stranger in a Strange Land"/> + </outline> + <outline text="Isaac Asimov"> + <outline text="Foundation and Empire"/> + </outline> + </outline> + <outline text="Qt Books (in English)"> + <outline text="Blanchette & Summerfield: C++ GUI Programming with Qt 3"/> + <outline text="Dalheimer: Programming with Qt"/> + <outline text="Griffith: KDE 2/Qt Programming Bible"/> + <outline text="Hughes: Linux Rapid Application Development"/> + <outline text="Solin: Qt Programming in 24 hours"/> + <outline text="Ward: Qt 2 Programming for Linux and Windows 2000"/> + </outline> + </outline> + <outline text="Shopping list"> + <outline text="General"> + <outline text="Towel"/> + <outline text="Hair dryer"/> + <outline text="Underpants"/> + </outline> + <outline text="For Sunday"> + <outline text="Beef"/> + <outline text="Rice"/> + <outline text="Carrots"/> + <outline text="Beans"/> + <outline text="Beer"/> + <outline text="Wine"/> + <outline text="Orange juice"/> + </outline> + </outline> + <outline text="Write a letter to Ford"> + </outline> + </body> +</opml> diff --git a/examples/xml/tagreader-with-features/fnord.xml b/examples/xml/tagreader-with-features/fnord.xml new file mode 100644 index 000000000..4d1303dd1 --- /dev/null +++ b/examples/xml/tagreader-with-features/fnord.xml @@ -0,0 +1,13 @@ +<document xmlns:book = 'http://trolltech.com/fnord/book/' + xmlns = 'http://trolltech.com/fnord/' > +<book> + <book:title>Practical XML</book:title> + <book:author xmlns:fnord = 'http://trolltech.com/fnord/' + title="Ms" + fnord:title="Goddess" + name="Eris Kallisti"/> + <chapter> + <title>A Namespace Called fnord</title> + </chapter> +</book> +</document> diff --git a/examples/xml/tagreader-with-features/structureparser.cpp b/examples/xml/tagreader-with-features/structureparser.cpp new file mode 100644 index 000000000..3bbd2a9c9 --- /dev/null +++ b/examples/xml/tagreader-with-features/structureparser.cpp @@ -0,0 +1,56 @@ +/* +*/ + +#include "structureparser.h" + +#include <qstring.h> +#include <qlistview.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; +} diff --git a/examples/xml/tagreader-with-features/structureparser.h b/examples/xml/tagreader-with-features/structureparser.h new file mode 100644 index 000000000..9ab8ec2af --- /dev/null +++ b/examples/xml/tagreader-with-features/structureparser.h @@ -0,0 +1,29 @@ +/* +*/ + +#ifndef STRUCTUREPARSER_H +#define STRUCTUREPARSER_H + +#include <qxml.h> +#include <qptrstack.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 diff --git a/examples/xml/tagreader-with-features/tagreader-with-features.pro b/examples/xml/tagreader-with-features/tagreader-with-features.pro new file mode 100644 index 000000000..ffe7648d7 --- /dev/null +++ b/examples/xml/tagreader-with-features/tagreader-with-features.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = tagreader-with-features + +CONFIG += qt warn_on release + +REQUIRES = xml large-config + +HEADERS = structureparser.h +SOURCES = tagreader.cpp \ + structureparser.cpp +INTERFACES = diff --git a/examples/xml/tagreader-with-features/tagreader.cpp b/examples/xml/tagreader-with-features/tagreader.cpp new file mode 100644 index 000000000..866879a4f --- /dev/null +++ b/examples/xml/tagreader-with-features/tagreader.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2005-2008 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 <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 ) +{ + 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(); +} diff --git a/examples/xml/tagreader-with-features/tagreader.doc b/examples/xml/tagreader-with-features/tagreader.doc new file mode 100644 index 000000000..1d11dc260 --- /dev/null +++ b/examples/xml/tagreader-with-features/tagreader.doc @@ -0,0 +1,40 @@ +/* +*/ + +/*! \page tagreader-with-features-example.html + + \ingroup xml-examples + + \title Demonstration of SAX2 features + + This example presents a small \link xml.html#sax2 SAX2 \endlink + 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 \link xml.html#sax2Namespaces features \endlink + \e http://xml.org/sax/features/namespaces and + \e http://xml.org/sax/features/namespace-prefixes are set. + + This example is thoroughly explained in a + \link xml-sax-features-walkthrough.html walkthrough. \endlink + + <hr> + + Header file: + + \include xml/tagreader-with-features/structureparser.h + + <hr> + + Implementation: + + \include xml/tagreader-with-features/structureparser.cpp + + <hr> + + Main: + + \include xml/tagreader-with-features/tagreader.cpp +*/ diff --git a/examples/xml/tagreader/animals.xml b/examples/xml/tagreader/animals.xml new file mode 100644 index 000000000..e1284901d --- /dev/null +++ b/examples/xml/tagreader/animals.xml @@ -0,0 +1,7 @@ +<animals> +<mammals> + <monkeys> <gorilla/> <orangutan/> </monkeys> +</mammals> +<birds> <pigeon/> <penguin/> </birds> +</animals> + diff --git a/examples/xml/tagreader/structureparser.cpp b/examples/xml/tagreader/structureparser.cpp new file mode 100644 index 000000000..9c13f4568 --- /dev/null +++ b/examples/xml/tagreader/structureparser.cpp @@ -0,0 +1,34 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 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 <qstring.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; +} diff --git a/examples/xml/tagreader/structureparser.h b/examples/xml/tagreader/structureparser.h new file mode 100644 index 000000000..c860f21a5 --- /dev/null +++ b/examples/xml/tagreader/structureparser.h @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 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 <qxml.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 diff --git a/examples/xml/tagreader/tagreader.cpp b/examples/xml/tagreader/tagreader.cpp new file mode 100644 index 000000000..93dd4cc59 --- /dev/null +++ b/examples/xml/tagreader/tagreader.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 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 <qfile.h> +#include <qxml.h> +#include <qwindowdefs.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; +} diff --git a/examples/xml/tagreader/tagreader.doc b/examples/xml/tagreader/tagreader.doc new file mode 100644 index 000000000..9a0bad01d --- /dev/null +++ b/examples/xml/tagreader/tagreader.doc @@ -0,0 +1,35 @@ +/* +*/ + +/*! \page tagreader-example.html + + \ingroup xml-examples + + \title A tiny SAX2 parser + + This example presents a small \link xml.html#sax2 SAX2 \endlink + 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 + \link xml-sax-walkthrough.html walkthrough. \endlink + + <hr> + + Header file: + + \include xml/tagreader/structureparser.h + + <hr> + + Implementation: + + \include xml/tagreader/structureparser.cpp + + <hr> + + Main: + + \include xml/tagreader/tagreader.cpp +*/ diff --git a/examples/xml/tagreader/tagreader.pro b/examples/xml/tagreader/tagreader.pro new file mode 100644 index 000000000..b6ebf8b97 --- /dev/null +++ b/examples/xml/tagreader/tagreader.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = tagreader + +CONFIG += qt console warn_on release + +REQUIRES = xml large-config + +HEADERS = structureparser.h +SOURCES = tagreader.cpp \ + structureparser.cpp +INTERFACES = |