1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Toolkit - xml/tagreader-with-features/tagreader.cpp example file
</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<h1 align=center>Demonstration of SAX2 features</h1><br clear="all">
This example presents a small <a href="xml-sax.html">SAX2</a>
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 <a href="xml-sax.html#namespaces">features</a>
http://xml.org/sax/features/namespaces and
http://xml.org/sax/features/namespace-prefixes are set.
<hr>
Header file: <pre>/*
$Id: qt/examples/xml/tagreader-with-features/structureparser.h 2.3.2 edited 2001-01-26 $
*/
#include <<a href="qxml-h.html">qxml.h</a>>
#include <<a href="qstack-h.html">qstack.h</a>>
class QListView;
class QListViewItem;
class QString;
class StructureParser: public QXmlDefaultHandler
{
public:
StructureParser( <a href="qlistview.html">QListView</a> * );
bool startDocument();
bool startElement( const QString&, const QString&, const QString& ,
const QXmlAttributes& );
bool endElement( const QString&, const QString&, const QString& );
private:
<a href="qstack.html">QStack</a><<a href="qlistviewitem.html">QListViewItem</a>> stack;
<a href="qlistview.html">QListView</a> * table;
};
</pre>
<hr>
Implementation: <pre>/*
$Id: qt/examples/xml/tagreader-with-features/structureparser.cpp 2.3.2 edited 2001-01-26 $
*/
#include "structureparser.h"
#include <<a href="qstring-h.html">qstring.h</a>>
#include <<a href="qlistview-h.html">qlistview.h</a>>
StructureParser::StructureParser( <a href="qlistview.html">QListView</a> * t )
: <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a>()
{
table = t;
table->setSorting( -1 ); // no sorting
table->addColumn( "Qualified name" );
table->addColumn( "Namespace" );
}
bool <a name="338"></a>StructureParser::startDocument()
{
return TRUE;
}
bool <a name="339"></a>StructureParser::startElement( const QString& namespaceURI, const QString& ,
const QString& qName,
const QXmlAttributes& attributes)
{
<a href="qlistviewitem.html">QListViewItem</a> * element;
if ( ! stack.isEmpty() ){
element = new <a href="qlistviewitem.html">QListViewItem</a>( stack.top(), qName, namespaceURI );
} else {
element = new <a href="qlistviewitem.html">QListViewItem</a>( table, qName, namespaceURI );
}
stack.push( element );
element-><a href="qlistviewitem.html#1c5a28">setOpen</a>( TRUE );
if ( attributes.length() > 0 ){
<a href="qlistviewitem.html">QListViewItem</a> * attribute;
for ( int i = 0 ; i < attributes.length(); i++ ){
attribute = new <a href="qlistviewitem.html">QListViewItem</a>( element,
attributes.qName(i),
attributes.uri(i) );
}
}
return TRUE;
}
bool <a name="340"></a>StructureParser::endElement( const QString&, const QString&, const QString& )
{
stack.pop();
return TRUE;
}
</pre>
<hr>
Main:
<pre>/*
$Id: qt/examples/xml/tagreader-with-features/tagreader.cpp 2.3.2 edited 2001-06-12 $
*/
#include "structureparser.h"
#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>
#include <<a name="qfile.h"></a><a href="qfile-h.html">qfile.h</a>>
#include <<a name="qxml.h"></a><a href="qxml-h.html">qxml.h</a>>
#include <<a name="qlistview.h"></a><a href="qlistview-h.html">qlistview.h</a>>
#include <<a name="qgrid.h"></a><a href="qgrid-h.html">qgrid.h</a>>
#include <<a name="qmainwindow.h"></a><a href="qmainwindow-h.html">qmainwindow.h</a>>
#include <<a name="qlabel.h"></a><a href="qlabel-h.html">qlabel.h</a>>
int main( int argc, char **argv )
{
<a name="QApplication"></a><a href="qapplication.html">QApplication</a> app( argc, argv );
<a name="QFile"></a><a href="qfile.html">QFile</a> xmlFile( argc == 2 ? argv[1] : "fnord.xml" );
<a name="QXmlInputSource"></a><a href="qxmlinputsource.html">QXmlInputSource</a> source( xmlFile );
<a name="QXmlSimpleReader"></a><a href="qxmlsimplereader.html">QXmlSimpleReader</a> reader;
<a name="QGrid"></a><a href="qgrid.html">QGrid</a> * container = new <a href="qgrid.html">QGrid</a>( 3 );
<a name="QListView"></a><a href="qlistview.html">QListView</a> * nameSpace = new <a href="qlistview.html">QListView</a>( container, "table_namespace" );
StructureParser * handlerNamespace = new StructureParser( nameSpace );
reader.<a name="setContentHandler"></a><a href="qxmlsimplereader.html#3ce23b">setContentHandler</a>( handlerNamespace );
reader.<a name="parse"></a><a href="qxmlsimplereader.html#2d6b2a">parse</a>( source );
<a href="qlistview.html">QListView</a> * namespacePrefix = new <a href="qlistview.html">QListView</a>( container, "table_namespace_prefix" );
StructureParser * handlerNamespacePrefix = new StructureParser( namespacePrefix );
reader.<a href="qxmlsimplereader.html#3ce23b">setContentHandler</a>( handlerNamespacePrefix );
reader.<a name="setFeature"></a><a href="qxmlsimplereader.html#1f7766">setFeature</a>( "http://xml.org/sax/features/namespaces", TRUE );
reader.<a href="qxmlsimplereader.html#1f7766">setFeature</a>( "http://xml.org/sax/features/namespace-prefixes", TRUE );
reader.<a href="qxmlsimplereader.html#2d6b2a">parse</a>( source );
<a href="qlistview.html">QListView</a> * prefix = new <a href="qlistview.html">QListView</a>( container, "table_prefix");
StructureParser * handlerPrefix = new StructureParser( prefix );
reader.<a href="qxmlsimplereader.html#3ce23b">setContentHandler</a>( handlerPrefix );
reader.<a href="qxmlsimplereader.html#1f7766">setFeature</a>( "http://xml.org/sax/features/namespaces", FALSE );
reader.<a href="qxmlsimplereader.html#1f7766">setFeature</a>( "http://xml.org/sax/features/namespace-prefixes", TRUE );
reader.<a href="qxmlsimplereader.html#2d6b2a">parse</a>( source );
<a name="QLabel"></a><a href="qlabel.html">QLabel</a> * namespaceLabel = new <a href="qlabel.html">QLabel</a>(
"Default:\n"
"http://xml.org/sax/features/namespaces: TRUE\n"
"http://xml.org/sax/features/namespace-prefixes: FALSE\n",
container );
<a href="qlabel.html">QLabel</a> * namespacePrefixLabel = new <a href="qlabel.html">QLabel</a>(
"\n"
"http://xml.org/sax/features/namespaces: TRUE\n"
"http://xml.org/sax/features/namespace-prefixes: TRUE\n",
container );
<a href="qlabel.html">QLabel</a> * prefixLabel = new <a href="qlabel.html">QLabel</a>(
"\n"
"http://xml.org/sax/features/namespaces: FALSE\n"
"http://xml.org/sax/features/namespace-prefixes: TRUE\n",
container );
app.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( container );
container-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - XML Tagreader");
container-><a name="show"></a><a href="qwidget.html#200ee5">show</a>();
return app.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|