Xerces-C++ -- Validation XML par DTD

BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 - 3 mars 2008 à 17:19
moifort Messages postés 3 Date d'inscription mardi 28 février 2006 Statut Membre Dernière intervention 14 octobre 2008 - 14 oct. 2008 à 15:49
Hello les gens!
Comment allez-vous? Les vacances d'hiver se sont bien passées?

Si vous avez l'habitude d'utiliser Xerces comme parser XML, j'aurais besoin de vos lumières, please.
J'aimerais savoir s'il était possible de valider un fichier XML à l'aide d'une DTD et de Xerces?

Pour les détails techniques, je suis en C++, sous Windows, et j'utilise un DOMBuilder pour parser mon fichier XML.
D'après la doc officielle, les fonctions loadGrammar(), getGrammar(), etc... pourrait m'aider...mais j'avoue que je ne comprends pas beaucoup la doc

Est-ce quelqu'un pourrait me faire profiter de ses lumières sur le sujet?

Merci d'avance!

Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...

4 réponses

BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 103
4 mars 2008 à 09:49
Yop!
Personne n'utilise Xerces? Suis-je "vieux-jeu" avec ma demande?

Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
moifort Messages postés 3 Date d'inscription mardi 28 février 2006 Statut Membre Dernière intervention 14 octobre 2008
14 oct. 2008 à 15:47
Voila un ptit bout de code qui test la validité de ton XML + DTD avec Xercesc. Je l'ai testé ca marche avec des fichiers XML qui contiennent la DTD et ceux qui utilisent une DTD externe.

// Entete du fichier
#include
#include
#include
#include
#include
#include
#if defined(XERCES_NEW_IOSTREAMS)
#include
#else
#include
#endif
#include <stdlib.h>
#include <string.h>


XERCES_CPP_NAMESPACE_USE

//Dans ton code
// Initialize the XML4C system
try
{
XMLPlatformUtils::Initialize();
}

catch (const XMLException& toCatch)
{
MessageBox("Error during initialization! Message:\n");
}


SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;

//
// Create a DTD validator to be used for our validation work. Then create
// a SAX parser object and pass it our validator. Then, according to what
// we were told on the command line, set it to validate or not. He owns
// the validator, so we have to allocate it.
//
int errorCount = 0;
DTDValidator* valToUse = new DTDValidator;
SAXParser* parser = new SAXParser(valToUse);
parser->setValidationScheme(valScheme);

//
// Get the starting time and kick off the parse of the indicated
// file. Catch any exceptions that might propogate out of it.
//
int errorCode = 0;
try
{
parser->parse("Logiconf.xml");
errorCount = parser->getErrorCount();
}
catch (const OutOfMemoryException&)
{
MessageBox("OutOfMemoryException");
errorCode = 5;
}
catch (const XMLException& e)
{
MessageBox("Error during parsing");
errorCode = 4;
}
if(errorCode) {
XMLPlatformUtils::Terminate();
MessageBox("Error code");
}

if (!errorCount) {
MessageBox(" XML + DTD = OK :)");
}
else
MessageBox("Ca marche poooooooooooooooooooo");

//
// Delete the parser itself. Must be done prior to calling Terminate, below.
//
delete parser;

// And call the termination method
XMLPlatformUtils::Terminate();


</string.h></stdlib.h>
0
moifort Messages postés 3 Date d'inscription mardi 28 février 2006 Statut Membre Dernière intervention 14 octobre 2008
14 oct. 2008 à 15:48
Voila un ptit bout de code qui test la validité de ton XML + DTD avec Xercesc. Je l'ai testé ca marche avec des fichiers XML qui contiennent la DTD et ceux qui utilisent une DTD externe.

// Entete du fichier
#include 
#include 
#include 
#include 
#include 
#include     
#if defined(XERCES_NEW_IOSTREAMS)
#include 
#else
#include 
#endif
#include <stdlib.h>
#include <string.h>


XERCES_CPP_NAMESPACE_USE 

//Dans ton code
// Initialize the XML4C system
    try
    {
         XMLPlatformUtils::Initialize();
    }

    catch (const XMLException&amp; toCatch)
    {
MessageBox("Error during initialization! Message:\n");
    }

   
    SAXParser::ValSchemes    valScheme = SAXParser::Val_Auto;

    //
    //  Create a DTD validator to be used for our validation work. Then create
    //  a SAX parser object and pass it our validator. Then, according to what
    //  we were told on the command line, set it to validate or not. He owns
    //  the validator, so we have to allocate it.
    //
    int errorCount = 0;
    DTDValidator* valToUse = new DTDValidator;
    SAXParser* parser = new SAXParser(valToUse);
    parser->setValidationScheme(valScheme);

    //
    //  Get the starting time and kick off the parse of the indicated
    //  file. Catch any exceptions that might propogate out of it.
    //
    int errorCode = 0;
    try
    {
        parser->parse("Logiconf.xml");
        errorCount = parser->getErrorCount();
    }
    catch (const OutOfMemoryException&amp;)
    {
        MessageBox("OutOfMemoryException");
        errorCode = 5;
    }
    catch (const XMLException&amp; e)
    {
MessageBox("Error during parsing"); 
errorCode = 4;
    }
    if(errorCode) {
        XMLPlatformUtils::Terminate();
MessageBox("Error code");
    }

    if (!errorCount) {
MessageBox(" XML + DTD = OK :)");
}
else
MessageBox("Ca marche poooooooooooooooooooo");

 //
    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
    //
    delete parser;

    // And call the termination method
    XMLPlatformUtils::Terminate();
</string.h></stdlib.h>
0
moifort Messages postés 3 Date d'inscription mardi 28 février 2006 Statut Membre Dernière intervention 14 octobre 2008
14 oct. 2008 à 15:49
Bo j'arrive pas a mettre en forme le code dsl...
0
Rejoignez-nous