Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QXmlSimpleReader Class Reference
[QtXml module]

The QXmlSimpleReader class provides an implementation of a simple XML parser. More...

 #include <QXmlSimpleReader>

Inherits QXmlReader.

Note: All the functions in this class are reentrant.

Public Functions


Detailed Description

The QXmlSimpleReader class provides an implementation of a simple XML parser.

This XML reader is suitable for a wide range of applications. It is able to parse well-formed XML and can report the namespaces of elements to a content handler; however, it does not parse any external entities.

The easiest pattern of use for this class is to create a reader instance, define an input source, specify the handlers to be used by the reader, and parse the data.

For example, we could use a QFile to supply the input. Here, we create a reader, and define an input source to be used by the reader:

     QXmlSimpleReader xmlReader;
     QXmlInputSource *source = new QXmlInputSource(file);

A handler lets us perform actions when the reader encounters certain types of content, or if errors in the input are found. The reader must be told which handler to use for each type of event. For many common applications, we can create a custom handler by subclassing QXmlDefaultHandler, and use this to handle both error and content events:

     Handler *handler = new Handler;
     xmlReader.setContentHandler(handler);
     xmlReader.setErrorHandler(handler);

If you don't set at least the content and error handlers, the parser will fall back on its default behavior---and will do nothing.

The most convenient way to handle the input is to read it in a single pass using the parse() function with an argument that specifies the input source:

     bool ok = xmlReader.parse(source);

     if (!ok)
         std::cout << "Parsing failed." << std::endl;

If you can't parse the entire input in one go (for example, it is huge, or is being delivered over a network connection), data can be fed to the parser in pieces. This is achieved by telling parse() to work incrementally, and making subsequent calls to the parseContinue() function, until all the data has been processed.

A common way to perform incremental parsing is to connect the readyRead() signal of the input source to a slot, and handle the incoming data there. For example, the following code shows how a parser for RSS feeds can be used to incrementally parse data that it receives from a QHttp object:

 void RSSListing::readData(const QHttpResponseHeader &resp)
 {
     bool ok;

     if (resp.statusCode() != 200)
         http.abort();
     else {
         xmlInput.setData(http.readAll());

         if (newInformation) {
             ok = xmlReader.parse(&xmlInput, true);
             newInformation = false;
         }
         else
             ok = xmlReader.parseContinue();

         if (!ok)
             http.abort();
     }
 }

Aspects of the parsing behavior can be adapted using setFeature() and setProperty(). For example, the following code could be used to enable reporting of namespace prefixes to the content handler:

 xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);

Member Function Documentation

QXmlSimpleReader::QXmlSimpleReader ()

Constructs a simple XML reader.

QXmlSimpleReader::~QXmlSimpleReader ()   [virtual]

Destroys the simple XML reader.

bool QXmlSimpleReader::parse ( const QXmlInputSource * input, bool incremental )   [virtual]

Reads an XML document from input and parses it. Returns true if the parsing is completed successfully; otherwise returns false, indicating that an error occurred.

If incremental is false, this function will return false if the XML file is not read completely. The parsing cannot be continued in this case.

If incremental is true, the parser does not return false if it reaches the end of the input before reaching the end of the XML file. Instead, it stores the state of the parser so that parsing can be continued later when more data is available. In such a case, you can use the function parseContinue() to continue with parsing. This class stores a pointer to the input source input and the parseContinue() function tries to read from that input source. Therefore, you should not delete the input source input until you no longer need to call parseContinue().

If this function is called with incremental set to true while an incremental parse is in progress, a new parsing session will be started, and the previous session will be lost.

See also parseContinue() and QTcpSocket.

bool QXmlSimpleReader::parseContinue ()   [virtual]

Continues incremental parsing, taking input from the QXmlInputSource that was specified with the most recent call to parse(). To use this function, you must have called parse() with the incremental argument set to true.

Returns false if a parsing error occurs; otherwise returns true, even if the end of the XML file has not been reached. You can continue parsing at a later stage by calling this function again when there is more data available to parse.

Calling this function when there is no data available in the input source indicates to the reader that the end of the XML file has been reached. If the input supplied up to this point was not well-formed then a parsing error occurs, and false is returned. If the input supplied was well-formed, true is returned. It is important to end the input in this way because it allows you to reuse the reader to parse other XML files.

Calling this function after the end of file has been reached, but without available data will cause false to be returned whether the previous input was well-formed or not.

See also parse(), QXmlInputSource::data(), and QXmlInputSource::next().

void QXmlSimpleReader::setFeature ( const QString & name, bool enable )   [virtual]

Turns on the feature name if enable is true; otherwise turns it off.

The name parameter must be one of the following strings:

FeatureDefaultNotes
http://xml.org/sax/features/namespacestrueIf enabled, namespaces are reported to the content handler.
http://xml.org/sax/features/namespace-prefixesfalseIf enabled, the original prefixed names and attributes used for namespace declarations are reported.
http://trolltech.com/xml/features/report-whitespace-only-CharDatatrueIf enabled, CharData that consist of only whitespace characters are reported using QXmlContentHandler::characters().
http://trolltech.com/xml/features/report-start-end-entityfalseIf enabled, the parser reports QXmlContentHandler::startEntity() and QXmlContentHandler::endEntity() events, so character data might be reported in chunks. If disabled, the parser does not report these events, but silently substitutes the entities, and reports the character data in one chunk.

Reimplemented from QXmlReader.

See also feature(), hasFeature(), and SAX2 Features.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le 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 utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

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 4.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 !
 
 
 
 
Partenaires

Hébergement Web