[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/xml-sax-features-walkthrough.doc:36 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Walkthrough: Using SAX2 features with the Qt XML classes</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Walkthrough: Using SAX2 features with the Qt XML classes</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | <p> This document assumes that you are familiar with <a href="xml.html#namespaces">namespaces</a> in XML and the concept of a <a href="xml.html#sax2">SAX2
|
---|
| 37 | parser</a>.
|
---|
| 38 | If features of SAX2 readers are new to you please read
|
---|
| 39 | <a href="xml.html#sax2Features">the feature section</a> of the SAX2 document.
|
---|
| 40 | <p> As a novice to the Qt XML classes it is advisable to have a look at the
|
---|
| 41 | <a href="xml-sax-walkthrough.html">tiny SAX2 parser walkthrough</a> before
|
---|
| 42 | reading on.
|
---|
| 43 | <p> This walkthrough covers two topics: First of all it shows how to
|
---|
| 44 | set SAX2 features and secondly how to integrate the Qt XML functionality
|
---|
| 45 | into a Qt GUI application.
|
---|
| 46 | <p> The resulting application allows you to compare the output of the reader
|
---|
| 47 | depending on how the two features
|
---|
| 48 | <em>http://xml.org/sax/features/namespace-prefixes</em>
|
---|
| 49 | and <em>http://xml.org/sax/features/namespaces</em> are set.
|
---|
| 50 | To do this it shows tree views of the read XML file
|
---|
| 51 | listing the qualified names of elements and attributes and the respective
|
---|
| 52 | namespace URIs.
|
---|
| 53 | <p> <h3>Setting features</h3>
|
---|
| 54 | <p>
|
---|
| 55 |
|
---|
| 56 | <p> Let's begin with the main program of the application. First the boring
|
---|
| 57 | part: we include all the classes we need:
|
---|
| 58 | <p> <pre> #include "structureparser.h"
|
---|
| 59 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 60 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
| 61 | #include <<a href="qxml-h.html">qxml.h</a>>
|
---|
| 62 | #include <<a href="qlistview-h.html">qlistview.h</a>>
|
---|
| 63 | #include <<a href="qgrid-h.html">qgrid.h</a>>
|
---|
| 64 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
| 65 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
| 66 | </pre>
|
---|
| 67 | <p> <a href="#structureparser.h">structureparser.h</a> contains the API of
|
---|
| 68 | the XML parser that we implement in <a href="#structureparser.cpp">structureparser.cpp.</a>
|
---|
| 69 | <p> <pre> int main( int argc, char **argv )
|
---|
| 70 | {
|
---|
| 71 | <a href="qapplication.html">QApplication</a> app( argc, argv );
|
---|
| 72 | </pre>
|
---|
| 73 | <p> As usual we then create a Qt application object and hand command line arguments
|
---|
| 74 | over to it.
|
---|
| 75 | <p> <pre> <a href="qfile.html">QFile</a> xmlFile( argc == 2 ? argv[1] : "fnord.xml" );
|
---|
| 76 | </pre>
|
---|
| 77 | <p> If the user runs the program with one filename as
|
---|
| 78 | an argument we process this file, otherwise we use the <em>fnord.xml</em> file from
|
---|
| 79 | the example directory for demonstration purposes.
|
---|
| 80 | <p> <pre> <a href="qxmlinputsource.html">QXmlInputSource</a> source( &xmlFile );
|
---|
| 81 | </pre>
|
---|
| 82 | <p> We use <em>xmlFile</em> as the XML Input Source...
|
---|
| 83 | <p> <pre> <a href="qxmlsimplereader.html">QXmlSimpleReader</a> reader;
|
---|
| 84 | </pre>
|
---|
| 85 | <p> ... and instantiate a <em>reader</em> object. Later we will manipulate its features
|
---|
| 86 | and thus influence how the XML data are read.
|
---|
| 87 | <p> <pre> <a href="qgrid.html">QGrid</a> * container = new <a href="qgrid.html">QGrid</a>( 3 );
|
---|
| 88 | </pre>
|
---|
| 89 | <p> Now let's think about presenting the output: As described in the
|
---|
| 90 | <a href="xml.html#sax2Features">Qt SAX2 documentation</a>
|
---|
| 91 | there are three valid combinations of <em>http://xml.org/sax/features/namespace-prefixes</em>
|
---|
| 92 | and <em>http://xml.org/sax/features/namespaces</em>: TRUE/TRUE, TRUE/FALSE and
|
---|
| 93 | FALSE/TRUE. To show the relevant output side by side of each other
|
---|
| 94 | and mark them with three labels makes up for a grid layout consisting
|
---|
| 95 | of three columns (and thus two lines).
|
---|
| 96 | <p> <pre> <a href="qlistview.html">QListView</a> * nameSpace = new <a href="qlistview.html">QListView</a>( container, "table_namespace" );
|
---|
| 97 | </pre>
|
---|
| 98 | <p> The most natural way of presenting XML elements is in a tree.
|
---|
| 99 | Thus we use a listview. Its name <em>nameSpace</em> indicates that this
|
---|
| 100 | one will be used to present the combination of <em>http://xml.org/sax/features/namespaces</em> being TRUE and
|
---|
| 101 | <em>http://xml.org/sax/features/namespace-prefixes</em>
|
---|
| 102 | being FALSE -- the default configuration of a <a href="qxmlsimplereader.html">QXmlSimpleReader</a>.
|
---|
| 103 | <p> Being the first grid entry the <em>nameSpace</em> listview will
|
---|
| 104 | appear in the upper left corner of the virtual grid.
|
---|
| 105 | <p> <pre> StructureParser * handler = new StructureParser( nameSpace );
|
---|
| 106 | </pre>
|
---|
| 107 | <p> Then we create a handler that deals with the XML data read by the reader.
|
---|
| 108 | As the provided handler class <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a> simply does nothing
|
---|
| 109 | with the data from the reader,
|
---|
| 110 | we can't use it right away. Instead we have to subclass our
|
---|
| 111 | own <a href="#structureparser.cpp">StructureParser</a> from it.
|
---|
| 112 | <p> <pre> reader.<a href="qxmlreader.html#setContentHandler">setContentHandler</a>( handler );
|
---|
| 113 | </pre>
|
---|
| 114 | <p> The <em>handler</em> serves as content handler for the reader. Note that
|
---|
| 115 | for simplicity reasons we don't register e.g. an error handler. Thus
|
---|
| 116 | our program will not complain about for example missing closing tags
|
---|
| 117 | in the parsed XML document.
|
---|
| 118 | <p> <pre> reader.<a href="qxmlsimplereader.html#parse">parse</a>( source );
|
---|
| 119 | </pre>
|
---|
| 120 | <p> Finally we parse the document with the reader's default feature settings.
|
---|
| 121 | <p> <pre> <a href="qlistview.html">QListView</a> * namespacePrefix = new <a href="qlistview.html">QListView</a>( container,
|
---|
| 122 | "table_namespace_prefix" );
|
---|
| 123 | </pre>
|
---|
| 124 | <p> Now we prepare for the parsing of the same XML input source with
|
---|
| 125 | different reader settings. The output will be presented in
|
---|
| 126 | a second <a href="qlistview.html">QListView</a>, <em>namespacePrefix</em>. As it is the second
|
---|
| 127 | member of the <em>container</em> grid it will appear in the middle of
|
---|
| 128 | the upper grid row.
|
---|
| 129 | <p> <pre> handler->setListView( namespacePrefix );
|
---|
| 130 | </pre>
|
---|
| 131 | <p> Then we ask the <em>handler</em> to present the data in the <em>namespacePrefix</em>
|
---|
| 132 | listview.
|
---|
| 133 | <p> <pre> <a name="x2125"></a> reader.<a href="qxmlsimplereader.html#setFeature">setFeature</a>( "http://xml.org/sax/features/namespace-prefixes",
|
---|
| 134 | TRUE );
|
---|
| 135 | </pre>
|
---|
| 136 | <p> Now we modify the behaviour of the <em>reader</em> and change
|
---|
| 137 | <em>http://xml.org/sax/features/namespace-prefixes</em> from the default FALSE
|
---|
| 138 | to TRUE. The <em>http://xml.org/sax/features/namespaces</em> feature has
|
---|
| 139 | still its default setting TRUE.
|
---|
| 140 | <p> <pre> source.<a href="qxmlinputsource.html#reset">reset</a>();
|
---|
| 141 | </pre>
|
---|
| 142 | <p> We have to reset the input source to make the new parsing start from the
|
---|
| 143 | beginning of the document again.
|
---|
| 144 | <p> <pre> reader.<a href="qxmlsimplereader.html#parse">parse</a>( source );
|
---|
| 145 | </pre>
|
---|
| 146 | <p> Finally we parse the XML file a second time with the changed reader
|
---|
| 147 | settings (TRUE/TRUE).
|
---|
| 148 | <p> <pre> <a href="qlistview.html">QListView</a> * prefix = new <a href="qlistview.html">QListView</a>( container, "table_prefix");
|
---|
| 149 | handler->setListView( prefix );
|
---|
| 150 | reader.<a href="qxmlsimplereader.html#setFeature">setFeature</a>( "http://xml.org/sax/features/namespaces", FALSE );
|
---|
| 151 | source.<a href="qxmlinputsource.html#reset">reset</a>();
|
---|
| 152 | reader.<a href="qxmlsimplereader.html#parse">parse</a>( source );
|
---|
| 153 | </pre>
|
---|
| 154 | <p> Next we prepare and use the upper right listview to show the reader results
|
---|
| 155 | with the feature setting <em>http://xml.org/sax/features/namespaces</em>
|
---|
| 156 | FALSE and <em>http://xml.org/sax/features/namespace-prefixes</em> TRUE.
|
---|
| 157 | <p> <pre> // namespace label
|
---|
| 158 | (void) new <a href="qlabel.html">QLabel</a>(
|
---|
| 159 | "Default:\n"
|
---|
| 160 | "http://xml.org/sax/features/namespaces: TRUE\n"
|
---|
| 161 | "http://xml.org/sax/features/namespace-prefixes: FALSE\n",
|
---|
| 162 | container );
|
---|
| 163 |
|
---|
| 164 | // namespace prefix label
|
---|
| 165 | (void) new <a href="qlabel.html">QLabel</a>(
|
---|
| 166 | "\n"
|
---|
| 167 | "http://xml.org/sax/features/namespaces: TRUE\n"
|
---|
| 168 | "http://xml.org/sax/features/namespace-prefixes: TRUE\n",
|
---|
| 169 | container );
|
---|
| 170 |
|
---|
| 171 | // prefix label
|
---|
| 172 | (void) new <a href="qlabel.html">QLabel</a>(
|
---|
| 173 | "\n"
|
---|
| 174 | "http://xml.org/sax/features/namespaces: FALSE\n"
|
---|
| 175 | "http://xml.org/sax/features/namespace-prefixes: TRUE\n",
|
---|
| 176 | container );
|
---|
| 177 | </pre>
|
---|
| 178 | <p> The second row of the <em>container</em> grid is filled with three labels
|
---|
| 179 | denoting the reader settings that belong to the above listview.
|
---|
| 180 | <p> <pre> app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( container );
|
---|
| 181 | container-><a href="qwidget.html#show">show</a>();
|
---|
| 182 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
| 183 | }
|
---|
| 184 | </pre>
|
---|
| 185 | <p> Same procedure as with every Qt GUI program: the grid serves as the
|
---|
| 186 | main widget of our application and is shown. After that we enter
|
---|
| 187 | the GUI's event loop.
|
---|
| 188 | <p> <h3><a name="structureparser.h">The handler API</a></h3>
|
---|
| 189 | <p> Let's have a brief look at the API of our handler class
|
---|
| 190 | <em>StructureParser</em>:
|
---|
| 191 | <p>
|
---|
| 192 |
|
---|
| 193 | <pre> #include <<a href="qxml-h.html">qxml.h</a>>
|
---|
| 194 | #include <<a href="qptrstack-h.html">qptrstack.h</a>>
|
---|
| 195 |
|
---|
| 196 | class QListView;
|
---|
| 197 | class QListViewItem;
|
---|
| 198 | class QString;
|
---|
| 199 | </pre>
|
---|
| 200 | <p> <pre> class StructureParser: public <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a>
|
---|
| 201 | {
|
---|
| 202 | </pre>
|
---|
| 203 | <p> We derive it from the <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a> class that
|
---|
| 204 | implements a handler that simply does nothing.
|
---|
| 205 | <p> <pre> public:
|
---|
| 206 | StructureParser( <a href="qlistview.html">QListView</a> * );
|
---|
| 207 | </pre>
|
---|
| 208 | <p> This makes it easy for us to implement only the functionality
|
---|
| 209 | we in fact need. In our case this is the constructor that
|
---|
| 210 | takes a <a href="qlistview.html">QListView</a> as an argument,
|
---|
| 211 | <p> <pre> bool startElement( const <a href="qstring.html">QString</a>&, const <a href="qstring.html">QString</a>&, const <a href="qstring.html">QString</a>& ,
|
---|
| 212 | const <a href="qxmlattributes.html">QXmlAttributes</a>& );
|
---|
| 213 | </pre>
|
---|
| 214 | <p> the function to execute at the occurrence of element start tags
|
---|
| 215 | (inherited from <a href="qxmlcontenthandler.html">QXmlContentHandler</a>), and
|
---|
| 216 | <p> <pre> bool endElement( const <a href="qstring.html">QString</a>&, const <a href="qstring.html">QString</a>&, const <a href="qstring.html">QString</a>& );
|
---|
| 217 | </pre>
|
---|
| 218 | <p> the code to run when an end tag occurs.
|
---|
| 219 | <p> All we have to implement so far is content handling.
|
---|
| 220 | <p> <pre> void setListView( <a href="qlistview.html">QListView</a> * );
|
---|
| 221 | </pre>
|
---|
| 222 | <p> In addition we have a function that selects a listview
|
---|
| 223 | for the output.
|
---|
| 224 | <p> <pre> private:
|
---|
| 225 | <a href="qptrstack.html">QPtrStack</a><QListViewItem> stack;
|
---|
| 226 | </pre>
|
---|
| 227 | <p> Keep in mind that we write a SAX2 parser that doesn't
|
---|
| 228 | have an object model to keep all elements and attributes
|
---|
| 229 | in memory. To display the elements and attributes in a tree like
|
---|
| 230 | structure we must however keep track of all elements
|
---|
| 231 | that haven't been closed yet.
|
---|
| 232 | <p> To do this we use a LIFO stack
|
---|
| 233 | of QListItems. An element will be added to the stack when
|
---|
| 234 | its start tag appears and removed
|
---|
| 235 | as soon as its end tag is parsed.
|
---|
| 236 | <p> <pre> <a href="qlistview.html">QListView</a> * table;
|
---|
| 237 | };
|
---|
| 238 | </pre>
|
---|
| 239 | <p> Apart from this we define a member variable that contains
|
---|
| 240 | the currently used listview.
|
---|
| 241 | <p> <h3><a name="structureparser.cpp">The handler itself</a></h3>
|
---|
| 242 | <p> Now that we defined the API we have to implement the
|
---|
| 243 | relevant functions.
|
---|
| 244 | <p>
|
---|
| 245 |
|
---|
| 246 | <pre> #include "structureparser.h"
|
---|
| 247 |
|
---|
| 248 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
| 249 | #include <<a href="qlistview-h.html">qlistview.h</a>>
|
---|
| 250 | </pre>
|
---|
| 251 | <p> <pre> StructureParser::StructureParser( <a href="qlistview.html">QListView</a> * t )
|
---|
| 252 | : <a href="qxmldefaulthandler.html">QXmlDefaultHandler</a>()
|
---|
| 253 | {
|
---|
| 254 | </pre>
|
---|
| 255 | <p> First we have the constructor that takes a listview pointer as
|
---|
| 256 | its argument.
|
---|
| 257 | <p> <pre> setListView( t );
|
---|
| 258 | }
|
---|
| 259 | </pre>
|
---|
| 260 | <p> All we have to do here is to prepare the argument <a href="qlistview.html">QListView</a>
|
---|
| 261 | before usage. This we do with the <a href="#setListView()">setListView()</a> function.
|
---|
| 262 | <p> <a name="setListView()"></a>
|
---|
| 263 | <pre> void StructureParser::setListView( <a href="qlistview.html">QListView</a> * t )
|
---|
| 264 | {
|
---|
| 265 | table = t;
|
---|
| 266 | </pre>
|
---|
| 267 | <p> First we store the argument away.
|
---|
| 268 | <p> <pre> table->setSorting( -1 );
|
---|
| 269 | </pre>
|
---|
| 270 | <p> We want the elements to be listed as they appear in the
|
---|
| 271 | document -- and not for example sorted alphabetically. That's
|
---|
| 272 | why we switch off sorting at all.
|
---|
| 273 | <p> <pre> table->addColumn( "Qualified name" );
|
---|
| 274 | table->addColumn( "Namespace" );
|
---|
| 275 | }
|
---|
| 276 | </pre>
|
---|
| 277 | <p> The listview now consists of two columns: one for the
|
---|
| 278 | element's or attribute's qualified names and one for
|
---|
| 279 | their namespace URIs. Columns are added from left to right
|
---|
| 280 | and with the title as an argument.
|
---|
| 281 | <p> Now let's deal with XML content handling.
|
---|
| 282 | <p> <pre> bool StructureParser::<a href="qxmlcontenthandler.html#startElement">startElement</a>( const <a href="qstring.html">QString</a>& namespaceURI,
|
---|
| 283 | const <a href="qstring.html">QString</a>& ,
|
---|
| 284 | const <a href="qstring.html">QString</a>& qName,
|
---|
| 285 | const <a href="qxmlattributes.html">QXmlAttributes</a>& attributes)
|
---|
| 286 | {
|
---|
| 287 | </pre>
|
---|
| 288 | <p> When we come across the start tag of an element the handler does
|
---|
| 289 | the real work. Although <em>startElement</em> is called with four
|
---|
| 290 | arguments we keep track of only three: the namespace URI
|
---|
| 291 | of the element, its qualified name and its attributes.
|
---|
| 292 | If an element has no namespace assigned or if the feature
|
---|
| 293 | settings of the reader don't provide the handler with
|
---|
| 294 | namespace URIs at all <em>namespaceURI</em> contains an empty
|
---|
| 295 | string.
|
---|
| 296 | <p> Note that we don't assign a variable to the second argument --
|
---|
| 297 | we're simply not interested in the local name of the element.
|
---|
| 298 | <p> <pre> <a href="qlistviewitem.html">QListViewItem</a> * element;
|
---|
| 299 | </pre>
|
---|
| 300 | <p> Whenever an element occurs we want to show it in the listview.
|
---|
| 301 | Therefore we define a <a href="qlistviewitem.html">QListViewItem</a> variable.
|
---|
| 302 | <p> <pre> if ( ! stack.isEmpty() ){
|
---|
| 303 | <a href="qlistviewitem.html">QListViewItem</a> *lastChild = stack.top()->firstChild();
|
---|
| 304 | </pre>
|
---|
| 305 | <p> As long as the element <em>stack</em> isn't empty the current element
|
---|
| 306 | is a child of the topmost (last unclosed) element on the stack. Thus we
|
---|
| 307 | create a new <a href="qlistviewitem.html">QListViewItem</a> as a child of QPtrStack::stack.top() with
|
---|
| 308 | the new element's qualified name in the first column and the according
|
---|
| 309 | namespace URI (or nothing) in the second one.
|
---|
| 310 | <p> The <a href="qlistviewitem.html">QListViewItem</a> is usally inserted as the first child. This means that we
|
---|
| 311 | would get the elements in reverse order. So we first search for the last
|
---|
| 312 | child of the QPtrStack::stack.top() element and insert it after this
|
---|
| 313 | element.
|
---|
| 314 | <p> In a valid XML document this applies to all elements except
|
---|
| 315 | the document root.
|
---|
| 316 | <p> <pre> if ( lastChild ) {
|
---|
| 317 | while ( lastChild-><a href="qlistviewitem.html#nextSibling">nextSibling</a>() )
|
---|
| 318 | lastChild = lastChild-><a href="qlistviewitem.html#nextSibling">nextSibling</a>();
|
---|
| 319 | }
|
---|
| 320 | element = new <a href="qlistviewitem.html">QListViewItem</a>( stack.top(), lastChild, qName, namespaceURI );
|
---|
| 321 | } else {
|
---|
| 322 | element = new <a href="qlistviewitem.html">QListViewItem</a>( table, qName, namespaceURI );
|
---|
| 323 | }
|
---|
| 324 | </pre>
|
---|
| 325 | <p> The root element we have to handle separately because it is
|
---|
| 326 | the first element to go onto the <a href="qlistviewitem.html">QListViewItem</a> stack.
|
---|
| 327 | Its listview item is therefore a direct child of the
|
---|
| 328 | <em>table</em> listview itself.
|
---|
| 329 | <p> <pre> stack.push( element );
|
---|
| 330 | </pre>
|
---|
| 331 | <p> Now we put the element's listview item on top of the stack.
|
---|
| 332 | <p> <pre> element-><a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE );
|
---|
| 333 | </pre>
|
---|
| 334 | <p> By default a <a href="qlistview.html">QListView</a> presents all of its nodes closed.
|
---|
| 335 | The user may then click on the <em>+</em> icon to see the child
|
---|
| 336 | entries.
|
---|
| 337 | <p> We however want to see the entire element tree
|
---|
| 338 | at once when we run the program.
|
---|
| 339 | Therefore we open each listview item manually.
|
---|
| 340 | <p> <pre> if ( attributes.<a href="qxmlattributes.html#length">length</a>() > 0 ) {
|
---|
| 341 | </pre>
|
---|
| 342 | <p> What do we do if an element has attributes?
|
---|
| 343 | <p> <pre> <a name="x2105"></a> for ( int i = 0 ; i < attributes.<a href="qxmlattributes.html#length">length</a>(); i++ ) {
|
---|
| 344 | <a name="x2107"></a><a name="x2106"></a> new <a href="qlistviewitem.html">QListViewItem</a>( element, attributes.<a href="qxmlattributes.html#qName">qName</a>(i), attributes.<a href="qxmlattributes.html#uri">uri</a>(i) );
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | </pre>
|
---|
| 348 | <p> For each of them we create a new listview item to present the attribute's
|
---|
| 349 | qualified name and the relevant namespace URI (or nothing).
|
---|
| 350 | Obviously <em>attribute</em> is a child of
|
---|
| 351 | the current <em>element</em>.
|
---|
| 352 | <p> <pre> return TRUE;
|
---|
| 353 | }
|
---|
| 354 | </pre>
|
---|
| 355 | <p> To prevent the reader from throwing an error we have to
|
---|
| 356 | return TRUE when we successfully dealt with an
|
---|
| 357 | element's start tag.
|
---|
| 358 | <p> <pre> bool StructureParser::<a href="qxmlcontenthandler.html#endElement">endElement</a>( const <a href="qstring.html">QString</a>&, const <a href="qstring.html">QString</a>&,
|
---|
| 359 | const <a href="qstring.html">QString</a>& )
|
---|
| 360 | {
|
---|
| 361 | stack.pop();
|
---|
| 362 | </pre>
|
---|
| 363 | <p> Whenever we come across an element's closing tag we
|
---|
| 364 | have to remove its listview item from the stack as
|
---|
| 365 | it can't have children any longer.
|
---|
| 366 | <p> <pre> return TRUE;
|
---|
| 367 | }
|
---|
| 368 | </pre>
|
---|
| 369 | <p> And so we're done.
|
---|
| 370 | <p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>.
|
---|
| 371 |
|
---|
| 372 | <!-- eof -->
|
---|
| 373 | <p><address><hr><div align=center>
|
---|
| 374 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 375 | <td>Copyright © 2007
|
---|
| 376 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 377 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 378 | </table></div></address></body>
|
---|
| 379 | </html>
|
---|