[Xerces-C++] Validation DTD
Bonjour,
J'utilise Xerces-C++ pour lire des fichiers XML et maintenant je voudrais faire valider mes fichiers avec une DTD. Bon, là j'ai ma DTD, je l'ai testée avec le logiciel xmlspy (en lui donnant un fichier qui ne la respecte pas) ça marche nickel.
Quand je suis passé à Xerces, j'ai lu sur la doc qu'il suffit d'activer l'option "fgSAX2CoreValidation" pour qu'il me fasse la validation. En gros le bout de code ressemble à ça :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
fprintf(stderr," erreur, le message d'exception est : %s \n", message);
XMLString::release(&message);
}
SAX2XMLReader* monParser;
if ( (monParser = XMLReaderFactory::createXMLReader()) == (SAX2XMLReader*) NULL)
{
fprintf(stderr," Erreur \n");
exit(EXIT_FAILURE);
}
monParser->setFeature(XMLUni::fgSAX2CoreValidation, true);
MySAX2Handler* mySAX2Handler = new MySAX2Handler(this);
monParser->setContentHandler(mySAX2Handler);
monParser->setErrorHandler(mySAX2Handler);
try
{
monParser->parse(XMLFile);
}
catch (const XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
fprintf(stderr, "erreur analyseur, le message d'exception est : %s \n", message);
XMLString::release(&message);
}
catch (const SAXParseException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
fprintf(stderr, "erreur analyseur, le message d'exception est : %s \n", message);
XMLString::release(&message);
}
catch (...)
{
fprintf(stderr, "exception innatendue\n");
} |
La lecture des fichiers XML marche, par contre la validation DTD pas du tout 8O. Est-ce que j'ai activé correctement la validation DTD ? sinon comment faire ?
Merci d'avance pour votre aide.
Validation XML - DTD Xerces
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| // Entete du fichier
#include <xercesc/util/NameIdPool.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/framework/XMLValidator.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/validators/DTD/DTDValidator.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#if defined(XERCES_NEW_IOSTREAMS)
#include <iostream>
#else
#include <iostream.h>
#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(); |