Re: Is it possible to validate an xml file (that refers to an xsd file) using the sax parser provided by libxml++ ?



2012-03-17 12:47, Franck Houssen skrev:

This example enables validation but uses a dom parser. The main steps of this example are :
xmlpp::DomParser parser ( xmlFile );
xmlpp::SchemaValidator validator ( xsdFile);
validator.validate( parser.get_document () );

I can't figure out how to modify this example to enable validation with xmlpp::SaxParser ? There is no get_document method neither on xmlpp:SaxParser nor on xmlpp:Parser.
I tried the following code but it seems not to work either :
xmlpp::SchemaValidator validator;
validator.parse_file ( xsdFile );
bool validDoc = validator.validate ( xmlFile ); // First validate
if ( !validDoc ) { cerr << "XmlChecker ERROR - " << xmlFile << " is not valid" << endl; return 1; }
MySaxParser aParser; // MySaxParser derives from xmlpp::SaxParser
aParser.set_validate ( true );
aParser.parse_file ( xmlFile ); // Then parse xml

I suppose you have noticed that the schemavalidation example checks for thrown exceptions instead of checking the return value from xmlpp::SchemaValidator::validate(). There's a good reason. The return value is absurd and useless. Both validate() functions return false (!) if the document is valid, and throw an exception if it's invalid.

You could file a bug about this. At least the strange behaviour should be documented.

Kjell



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]