Walkthrough: Using SAX2 features with the Qt XML classes
This document assumes that you are familiar with namespaces in XML and the concept of a SAX2 parser. If features of SAX2 readers are new to you please read the feature section of the SAX2 document. As a novice to the Qt XML classes it is advisable to have a look at the tiny SAX2 parser walkthrough before reading on. This walkthrough covers two topics: First of all it shows how to set SAX2 features and secondly how to integrate the Qt XML functionality into a Qt GUI application. The resulting application allows you to compare the output of the reader depending on how the two features http://xml.org/sax/features/namespace-prefixes and http://xml.org/sax/features/namespaces are set. To do this it shows tree views of the read XML file listing the qualified names of elements and attributes and the respective namespace URIs.
Setting features
Let's begin with the main program of the application. First the boring part: we include all the classes we need:
#include "structureparser.h" #include <qapplication.h> #include <qfile.h> #include <qxml.h> #include <qlistview.h> #include <qgrid.h> #include <qmainwindow.h> #include <qlabel.h> structureparser.h contains the API of the XML parser that we implement in structureparser.cpp.
int main( int argc, char **argv ) { QApplication app( argc, argv ); As usual we then create a Qt application object and hand command line arguments over to it.
QFile xmlFile( argc == 2 ? argv[1] : "fnord.xml" ); If the user runs the program with one filename as an argument we process this file, otherwise we use the fnord.xml file from the example directory for demonstration purposes.
QXmlInputSource source( &xmlFile ); We use xmlFile as the XML Input Source...
QXmlSimpleReader reader; ... and instantiate a reader object. Later we will manipulate its features and thus influence how the XML data are read.
QGrid * container = new QGrid( 3 ); Now let's think about presenting the output: As described in the Qt SAX2 documentation there are three valid combinations of http://xml.org/sax/features/namespace-prefixes and http://xml.org/sax/features/namespaces: TRUE/TRUE, TRUE/FALSE and FALSE/TRUE. To show the relevant output side by side of each other and mark them with three labels makes up for a grid layout consisting of three columns (and thus two lines).
QListView * nameSpace = new QListView( container, "table_namespace" ); The most natural way of presenting XML elements is in a tree. Thus we use a listview. Its name nameSpace indicates that this one will be used to present the combination of http://xml.org/sax/features/namespaces being TRUE and http://xml.org/sax/features/namespace-prefixes being FALSE -- the default configuration of a QXmlSimpleReader. Being the first grid entry the nameSpace listview will appear in the upper left corner of the virtual grid.
StructureParser * handler = new StructureParser( nameSpace ); Then we create a handler that deals with the XML data read by the reader. As the provided handler class QXmlDefaultHandler simply does nothing with the data from the reader, we can't use it right away. Instead we have to subclass our own StructureParser from it.
The handler serves as content handler for the reader. Note that for simplicity reasons we don't register e.g. an error handler. Thus our program will not complain about for example missing closing tags in the parsed XML document.
Finally we parse the document with the reader's default feature settings.
QListView * namespacePrefix = new QListView( container, "table_namespace_prefix" ); Now we prepare for the parsing of the same XML input source with different reader settings. The output will be presented in a second QListView, namespacePrefix. As it is the second member of the container grid it will appear in the middle of the upper grid row.
handler->setListView( namespacePrefix ); Then we ask the handler to present the data in the namespacePrefix listview.
Now we modify the behaviour of the reader and change http://xml.org/sax/features/namespace-prefixes from the default FALSE to TRUE. The http://xml.org/sax/features/namespaces feature has still its default setting TRUE.
We have to reset the input source to make the new parsing start from the beginning of the document again.
reader.parse( source ); Finally we parse the XML file a second time with the changed reader settings (TRUE/TRUE).
QListView * prefix = new QListView( container, "table_prefix"); handler->setListView( prefix ); reader.setFeature( "http://xml.org/sax/features/namespaces", FALSE ); source.reset(); reader.parse( source ); Next we prepare and use the upper right listview to show the reader results with the feature setting http://xml.org/sax/features/namespaces FALSE and http://xml.org/sax/features/namespace-prefixes TRUE.
// namespace label (void) new QLabel( "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 QLabel( "\n" "http://xml.org/sax/features/namespaces: TRUE\n" "http://xml.org/sax/features/namespace-prefixes: TRUE\n", container ); // prefix label (void) new QLabel( "\n" "http://xml.org/sax/features/namespaces: FALSE\n" "http://xml.org/sax/features/namespace-prefixes: TRUE\n", container ); The second row of the container grid is filled with three labels denoting the reader settings that belong to the above listview.
app.setMainWidget( container ); container->show(); return app.exec(); } Same procedure as with every Qt GUI program: the grid serves as the main widget of our application and is shown. After that we enter the GUI's event loop.
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Developer Network au hasardGénération de bindings PySide avec ShibokenLe Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com