?? xml-tagreader-with-features-tagreader-cpp.html
字號:
<!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> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></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.8 edited 2004-05-12 $*/ #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.8 edited 2004-05-12 $*/#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="131"></a>StructureParser::startDocument(){ return TRUE;}bool <a name="132"></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="133"></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.8 edited 2004-05-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
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -