<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt - 3.3.8 - espenr - 2499/qt - x11 - free - 3.3.8/examples/xml/outliner/outliner.doc:5 -->
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title > Outliner to show use of DOM< / title >
< style type = "text/css" > < ! - -
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
-->< / style >
< / head >
< body >
< table border = "0" cellpadding = "0" cellspacing = "0" width = "100%" >
< tr bgcolor = "#E5E5E5" >
< td valign = center >
< a href = "index.html" >
< font color = "#004faf" > Home< / font > < / a >
| < a href = "classes.html" >
< font color = "#004faf" > All Classes< / font > < / a >
| < a href = "mainclasses.html" >
< font color = "#004faf" > Main Classes< / font > < / a >
| < a href = "annotated.html" >
< font color = "#004faf" > Annotated< / font > < / a >
| < a href = "groups.html" >
< font color = "#004faf" > Grouped Classes< / font > < / a >
| < a href = "functions.html" >
< font color = "#004faf" > Functions< / font > < / a >
< / td >
< td align = "right" valign = "center" > < img src = "logo32.png" align = "right" width = "64" height = "32" border = "0" > < / td > < / tr > < / table > < h1 align = center > Outliner to show use of DOM< / h1 >
< p >
< p > This example presents a small outliner program to show the basic usage of
the < a href = "xml.html#dom" > DOM classes< / a > . The format of the outlines
is the OPML format as described in http://www.opml.org/spec.
< p > This example shows how to load a DOM tree from an XML file and how to
traverse it.
< p > < hr >
< p > Sample XML file (todos.opml):
< p > < pre > < ?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="TQt Books (in English)">
< outline text="Blanchette & amp; Summerfield: C++ GUI Programming with TQt 3"/>
< outline text="Dalheimer: Programming with TQt"/>
< outline text="Griffith: KDE 2/TQt Programming Bible"/>
< outline text="Hughes: Linux Rapid Application Development"/>
< outline text="Solin: < a href = "ntqt.html" > TQt< / a > Programming in 24 hours"/>
< outline text="Ward: < a href = "ntqt.html" > TQt< / a > 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>
< / pre >
< p > < hr >
< p > Header file (outlinetree.h):
< p > < pre > /****************************************************************************
** $Id: qt/outlinetree.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 OUTLINETREE_H
#define OUTLINETREE_H
#include < < a href = "qlistview-h.html" > ntqlistview.h< / a > >
#include < < a href = "qdom-h.html" > ntqdom.h< / a > >
class OutlineTree : public < a href = "ntqlistview.html" > TQListView< / a >
{
< a href = "metaobjects.html#TQ_OBJECT" > TQ_OBJECT< / a >
public:
OutlineTree( const < a href = "ntqstring.html" > TQString< / a > fileName, TQWidget *parent = 0, const char *name = 0 );
~OutlineTree();
private:
< a href = "qdomdocument.html" > TQDomDocument< / a > domTree;
void getHeaderInformation( const < a href = "qdomelement.html" > TQDomElement< / a > & header );
void buildTree( < a href = "qlistviewitem.html" > TQListViewItem< / a > *parentItem, const < a href = "qdomelement.html" > TQDomElement< / a > & parentElement );
};
#endif // OUTLINETREE_H
< / pre >
< p > < hr >
< p > Implementation (outlinetree.cpp):
< p > < pre > /****************************************************************************
** $Id: qt/outlinetree.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 "outlinetree.h"
#include < < a href = "qfile-h.html" > ntqfile.h< / a > >
#include < < a href = "qmessagebox-h.html" > ntqmessagebox.h< / a > >
< a name = "f522" > < / a > OutlineTree::OutlineTree( const < a href = "ntqstring.html" > TQString< / a > fileName, TQWidget *parent, const char *name )
: < a href = "ntqlistview.html" > TQListView< / a > ( parent, name )
{
// div. configuration of the list view
< a href = "ntqlistview.html#addColumn" > addColumn< / a > ( "Outlines" );
< a href = "ntqlistview.html#setSorting" > setSorting< / a > ( -1 );
< a href = "ntqlistview.html#setRootIsDecorated" > setRootIsDecorated< / a > ( TRUE );
// read the XML file and create DOM tree
< a href = "ntqfile.html" > TQFile< / a > opmlFile( fileName );
< a name = "x1915" > < / a > if ( !opmlFile.< a href = "ntqfile.html#open" > open< / a > ( < a href = "ntqfile.html#open" > IO_ReadOnly< / a > ) ) {
< a name = "x1917" > < / a > TQMessageBox::< a href = "ntqmessagebox.html#critical" > critical< / a > ( 0,
< a href = "ntqobject.html#tr" > tr< / a > ( "Critical Error" ),
< a href = "ntqobject.html#tr" > tr< / a > ( "Cannot open file %1" ).arg( fileName ) );
return;
}
if ( !domTree.setContent( & opmlFile ) ) {
TQMessageBox::< a href = "ntqmessagebox.html#critical" > critical< / a > ( 0,
< a href = "ntqobject.html#tr" > tr< / a > ( "Critical Error" ),
< a href = "ntqobject.html#tr" > tr< / a > ( "Parsing error for file %1" ).arg( fileName ) );
< a name = "x1914" > < / a > opmlFile.< a href = "ntqfile.html#close" > close< / a > ();
return;
}
opmlFile.< a href = "ntqfile.html#close" > close< / a > ();
// get the header information from the DOM
< a href = "qdomelement.html" > TQDomElement< / a > root = domTree.documentElement();
< a href = "qdomnode.html" > TQDomNode< / a > node;
< a name = "x1907" > < / a > node = root.< a href = "qdomnode.html#firstChild" > firstChild< / a > ();
< a name = "x1909" > < / a > while ( !node.< a href = "qdomnode.html#isNull" > isNull< / a > () ) {
< a name = "x1911" > < / a > < a name = "x1908" > < / a > if ( node.< a href = "qdomnode.html#isElement" > isElement< / a > () & & node.< a href = "qdomnode.html#nodeName" > nodeName< / a > () == "head" ) {
< a name = "x1913" > < / a > < a href = "qdomelement.html" > TQDomElement< / a > header = node.< a href = "qdomnode.html#toElement" > toElement< / a > ();
getHeaderInformation( header );
break;
}
< a name = "x1910" > < / a > node = node.< a href = "qdomnode.html#nextSibling" > nextSibling< / a > ();
}
// create the tree view out of the DOM
node = root.< a href = "qdomnode.html#firstChild" > firstChild< / a > ();
while ( !node.< a href = "qdomnode.html#isNull" > isNull< / a > () ) {
if ( node.< a href = "qdomnode.html#isElement" > isElement< / a > () & & node.< a href = "qdomnode.html#nodeName" > nodeName< / a > () == "body" ) {
< a href = "qdomelement.html" > TQDomElement< / a > body = node.< a href = "qdomnode.html#toElement" > toElement< / a > ();
buildTree( 0, body );
break;
}
node = node.< a href = "qdomnode.html#nextSibling" > nextSibling< / a > ();
}
}
OutlineTree::~OutlineTree()
{
}
void < a name = "f523" > < / a > OutlineTree::getHeaderInformation( const < a href = "qdomelement.html" > TQDomElement< / a > & header )
{
// visit all children of the header element and look if you can make
// something with it
< a href = "qdomnode.html" > TQDomNode< / a > node = header.< a href = "qdomnode.html#firstChild" > firstChild< / a > ();
while ( !node.< a href = "qdomnode.html#isNull" > isNull< / a > () ) {
if ( node.< a href = "qdomnode.html#isElement" > isElement< / a > () ) {
// case for the different header entries
if ( node.< a href = "qdomnode.html#nodeName" > nodeName< / a > () == "title" ) {
< a href = "qdomtext.html" > TQDomText< / a > textChild = node.< a href = "qdomnode.html#firstChild" > firstChild< / a > ().toText();
if ( !textChild.< a href = "qdomnode.html#isNull" > isNull< / a > () ) {
< a name = "x1912" > < / a > < a href = "ntqlistview.html#setColumnText" > setColumnText< / a > ( 0, textChild.< a href = "qdomnode.html#nodeValue" > nodeValue< / a > () );
}
}
}
node = node.< a href = "qdomnode.html#nextSibling" > nextSibling< / a > ();
}
}
void < a name = "f524" > < / a > OutlineTree::buildTree( < a href = "qlistviewitem.html" > TQListViewItem< / a > *parentItem, const < a href = "qdomelement.html" > TQDomElement< / a > & parentElement )
{
< a href = "qlistviewitem.html" > TQListViewItem< / a > *thisItem = 0;
< a href = "qdomnode.html" > TQDomNode< / a > node = parentElement.< a href = "qdomnode.html#firstChild" > firstChild< / a > ();
while ( !node.< a href = "qdomnode.html#isNull" > isNull< / a > () ) {
if ( node.< a href = "qdomnode.html#isElement" > isElement< / a > () & & node.< a href = "qdomnode.html#nodeName" > nodeName< / a > () == "outline" ) {
// add a new list view item for the outline
if ( parentItem == 0 )
thisItem = new < a href = "qlistviewitem.html" > TQListViewItem< / a > ( this, thisItem );
else
thisItem = new < a href = "qlistviewitem.html" > TQListViewItem< / a > ( parentItem, thisItem );
< a name = "x1916" > < / a > thisItem-> < a href = "qlistviewitem.html#setText" > setText< / a > ( 0, node.< a href = "qdomnode.html#toElement" > toElement< / a > ().attribute( "text" ) );
// recursive build of the tree
buildTree( thisItem, node.< a href = "qdomnode.html#toElement" > toElement< / a > () );
}
node = node.< a href = "qdomnode.html#nextSibling" > nextSibling< / a > ();
}
}
< / pre >
< p > < hr >
< p > Main (main.cpp):
< p > < pre > /****************************************************************************
** $Id: qt/main.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 < < a href = "qapplication-h.html" > ntqapplication.h< / a > >
#include "outlinetree.h"
int main( int argc, char **argv )
{
< a href = "ntqapplication.html" > TQApplication< / a > a( argc, argv );
OutlineTree outline( "todos.opml" );
a.< a href = "ntqapplication.html#setMainWidget" > setMainWidget< / a > ( & outline );
< a name = "x1920" > < / a > outline.< a href = "ntqwidget.html#show" > show< / a > ();
return a.< a href = "ntqapplication.html#exec" > exec< / a > ();
}
< / pre >
< p > See also < a href = "xml-examples.html" > TQt XML Examples< / a > .
<!-- eof -->
< p > < address > < hr > < div align = center >
< table width = 100% cellspacing = 0 border = 0 > < tr >
< td > Copyright © 2007
< a href = "troll.html" > Trolltech< / a > < td align = center > < a href = "trademarks.html" > Trademarks< / a >
< td align = right > < div align = right > TQt 3.3.8< / div >
< / table > < / div > < / address > < / body >
< / html >