Walkthrough: How to use the Qt SAX2 classesFor a general discussion of the XML topics in Qt please refer to the document Qt XML Module. To learn more about SAX2 see the document describing the Qt SAX2 implementation. Before reading on you should at least be familiar with the Introduction to SAX2.
In this section we will present a small example reader that outputs
the names of all elements in an XML document on the command line.
The element names are indented corresponding to their nesting level.
As mentioned in Introduction to SAX2
we have to implement the functions of the handler classes that we are
interested in. In our case these are only three:
QXmlContentHandler::startDocument(),
QXmlContentHandler::startElement() and
QXmlContentHandler::endElement().
For this purpose we use a subclass of the QXmlDefaultHandler (remember
that the special handler classes are all abstract and the default handler class
provides an implementation that does not change the parsing behavior):
Apart from the private helper variable indent that we will use to
get indentation right, there is nothing special about our new
StructureParser class.
Even the implementation is straight-forward:
First we overload QXmlContentHandler::startElement() with a non-empty version.
At the beginning of the document we simply
set indent to an empty string because we
want to print out the root element without any indentation.
Also we return TRUE so that the parser continues without
reporting an error.
Because we want to be informed when the parser comes
accross a start tag of an element and subsequently print it out, we
have to overload QXmlContentHandler::startElement().
This is what the implementation does: The name of the element with
preceding indentation is printed out followed by a linebreak.
Strictly speaking qName contains the local element name
without an eventual prefix denoting the
namespace.
If another element follows before the current element's end tag
it should be indented. Therefore we add four spaces to the
indent string.
Finally we return TRUE in order to let the parser continue without
errors.
The last functionality we need to add is the parser's behaviour when an
end tag occurs. This means overloading QXmlContentHandler::endElement().
Obviously we then should shorten the indent string by the four
whitespaces added in startElement().
With this we're done with our parser and can start writing the main()
program.
Successively we deal with all files given as command line arguments.
The next step is to create an instance of the StructureParser.
Then we create a
QXmlInputSource for the XML file to be parsed.
After that we set up the reader. As our StructureParser
class deals with QXmlContentHandler functionality only
we simply register it as the content handler of our choice.
Now we take our input source and start parsing.
Running the program on the following XML file...
... produces the following output:
It will however refuse to produce the correct result if you e.g. insert
a whitespace between a < and the element name in your test-XML file.
To prevent such annoyances
you should always install an error handler with QXmlReader::setErrorHandler(). This allows you to report
parsing errors to the user.
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Labs au hasardChaînes et SIMD, la revanche (de Latin1)Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. 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 2.3 | |
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