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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

XML Schema Validation Example

Files:

This example shows how to use QtXmlPatterns to validate XML with a W3C XML Schema.

Introduction

The example application shows different XML schema definitions and for every definition two XML instance documents, one that is valid according to the schema and one that is not. The user can select the valid or invalid instance document, change it and validate it again.

The User Interface

The UI for this example was created using Qt Designer:

The UI consists of three parts, at the top the XML schema selection and the schema viewer, below the XML instance selection and the instance editor and at the bottom the validation status label next to the validation button.

Validating XML Instance Documents

You can select one of the three predefined XML schemas and for each schema an valid or invalid instance document. A click on the 'Validate' button will validate the content of the XML instance editor against the schema from the XML schema viewer. As you can modify the content of the instance editor, different instances can be tested and validation error messages analysed.

Code Walk-Through

The example's main() function creates the standard instance of QApplication. Then it creates an instance of the mainwindow class, shows it, and starts the Qt event loop:

 int main(int argc, char* argv[])
 {
     Q_INIT_RESOURCE(schema);
     QApplication app(argc, argv);
     MainWindow* const window = new MainWindow;
     window->show();
     return app.exec();
 }

The UI Class: MainWindow

The example's UI is a conventional Qt GUI application inheriting QMainWindow and the class generated by Qt Designer:

 class MainWindow : public QMainWindow,
                    private Ui::SchemaMainWindow
 {
     Q_OBJECT

   public:
     MainWindow();

   private Q_SLOTS:
     void schemaSelected(int index);
     void instanceSelected(int index);
     void validate();
     void textChanged();

   private:
     void moveCursor(int line, int column);
 };

The constructor fills the schema and instance QComboBox selections with the predefined schemas and instances and connects their currentIndexChanged() signals to the window's schemaSelected() resp. instanceSelected() slot. Furthermore the signal-slot connections for the validation button and the instance editor are set up.

The call to schemaSelected(0) and instanceSelected(0) will trigger the validation of the initial Contact Schema example.

 MainWindow::MainWindow()
 {
     setupUi(this);

     new XmlSyntaxHighlighter(schemaView->document());
     new XmlSyntaxHighlighter(instanceEdit->document());

     schemaSelection->addItem(tr("Contact Schema"));
     schemaSelection->addItem(tr("Recipe Schema"));
     schemaSelection->addItem(tr("Order Schema"));

     instanceSelection->addItem(tr("Valid Contact Instance"));
     instanceSelection->addItem(tr("Invalid Contact Instance"));

     connect(schemaSelection, SIGNAL(currentIndexChanged(int)), SLOT(schemaSelected(int)));
     connect(instanceSelection, SIGNAL(currentIndexChanged(int)), SLOT(instanceSelected(int)));
     connect(validateButton, SIGNAL(clicked()), SLOT(validate()));
     connect(instanceEdit, SIGNAL(textChanged()), SLOT(textChanged()));

     validationStatus->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);

     schemaSelected(0);
     instanceSelected(0);
 }

In the schemaSelected() slot the content of the instance selection is adapted to the selected schema and the corresponding schema is loaded from the resource file and displayed in the schema viewer. At the end of the method a revalidation is triggered.

 void MainWindow::schemaSelected(int index)
 {
     instanceSelection->clear();
     if (index == 0) {
         instanceSelection->addItem(tr("Valid Contact Instance"));
         instanceSelection->addItem(tr("Invalid Contact Instance"));
     } else if (index == 1) {
         instanceSelection->addItem(tr("Valid Recipe Instance"));
         instanceSelection->addItem(tr("Invalid Recipe Instance"));
     } else if (index == 2) {
         instanceSelection->addItem(tr("Valid Order Instance"));
         instanceSelection->addItem(tr("Invalid Order Instance"));
     }
     textChanged();

     QFile schemaFile(QString(":/schema_%1.xsd").arg(index));
     schemaFile.open(QIODevice::ReadOnly);
     const QString schemaText(QString::fromUtf8(schemaFile.readAll()));
     schemaView->setPlainText(schemaText);

     validate();
 }

In the instanceSelected() slot the selected instance is loaded from the resource file and loaded into the instance editor an the revalidation is triggered again.

 void MainWindow::instanceSelected(int index)
 {
     QFile instanceFile(QString(":/instance_%1.xml").arg((2*schemaSelection->currentIndex()) + index));
     instanceFile.open(QIODevice::ReadOnly);
     const QString instanceText(QString::fromUtf8(instanceFile.readAll()));
     instanceEdit->setPlainText(instanceText);

     validate();
 }

The validate() slot does the actual work in this example. At first it stores the content of the schema viewer and the editor into temporary variables. Then it instanciates a MessageHandler object which inherits from QAbstractMessageHandler and is a convenience class to store error messages from the XmlPatterns system.

 class MessageHandler : public QAbstractMessageHandler
 {
     public:
         MessageHandler()
             : QAbstractMessageHandler(0)
         {
         }

         QString statusMessage() const
         {
             return m_description;
         }

         int line() const
         {
             return m_sourceLocation.line();
         }

         int column() const
         {
             return m_sourceLocation.column();
         }

     protected:
         virtual void handleMessage(QtMsgType type, const QString &description,
                                    const QUrl &identifier, const QSourceLocation &sourceLocation)
         {
             Q_UNUSED(type);
             Q_UNUSED(identifier);

             m_messageType = type;
             m_description = description;
             m_sourceLocation = sourceLocation;
         }

     private:
         QtMsgType m_messageType;
         QString m_description;
         QSourceLocation m_sourceLocation;
 };

After the QXmlSchema is instanciated and the message handler set on it, the load() method is called with the schema data as argument. If the schema is invalid or a parsing error has occured, isValid() returns false and the error is flagged in errorOccurred. If the loading was successful, a QXmlSchemaValidator is instanciated and the schema passed in the constructor. A call to validate() will validate the passed XML instance data against the XML schema. The return value of that method signals whether the validation was successful. Depending on the success the status label is set to 'validation successful' or the error message stored in the MessageHandler

The rest of the code does only some fancy coloring and eyecandy.

 void MainWindow::validate()
 {
     const QByteArray schemaData = schemaView->toPlainText().toUtf8();
     const QByteArray instanceData = instanceEdit->toPlainText().toUtf8();

     MessageHandler messageHandler;

     QXmlSchema schema;
     schema.setMessageHandler(&messageHandler);

     schema.load(schemaData);

     bool errorOccurred = false;
     if (!schema.isValid()) {
         errorOccurred = true;
     } else {
         QXmlSchemaValidator validator(schema);
         if (!validator.validate(instanceData))
             errorOccurred = true;
     }

     if (errorOccurred) {
         validationStatus->setText(messageHandler.statusMessage());
         moveCursor(messageHandler.line(), messageHandler.column());
     } else {
         validationStatus->setText(tr("validation successful"));
     }

     const QString styleSheet = QString("QLabel {background: %1; padding: 3px}")
                                       .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() :
                                                            QColor(Qt::green).lighter(160).name());
     validationStatus->setStyleSheet(styleSheet);
 }
Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  4. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 12
  5. 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
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
Page suivante

Le Qt Developer Network au hasard

Logo

Compiler l'add-in Qt de Visual Studio

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.7
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