You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Franck Houssen <fh...@hotmail.com> on 2012/03/19 13:29:29 UTC

How to validate a xml file that refers to an xsd file using sax parser provided by Xerces-C++ ?

Hello,

I try without success to validate a xml file that refers to an xsd file using sax parser provided by Xerces-C++ ?

The following code does not work ( parsing a not well-formed xml will report error as expected, but an invalid xml - with respect to xsd - will not report error ) :
XMLPlatformUtils::Initialize ();
SAXParser * pParser = new SAXParser ();
if ( pParser )
{
  pParser -> setDoSchema ( true );
  pParser -> setLoadSchema ( true );
  pParser -> setExternalNoNamespaceSchemaLocation ( xsdFile.c_str () );
  pParser -> setValidationScheme ( SAXParser::Val_Auto );
  pParser -> setValidationSchemaFullChecking ( true );
  pParser -> setValidationConstraintFatal ( true );
      
  SaxParserHandler handler; // SaxParserHandler derives from HandlerBase
  pParser -> setDocumentHandler ( &handler );
  pParser -> setErrorHandler ( &handler );
  pParser -> parse ( xmlFile.c_str () );
      
  delete pParser; pParser = NULL;
}
XMLPlatformUtils::Terminate ();

I want the parse to stop if the xml is not compliant with the xsd. Bonus if I don't have to handle "by hand" the xsd ( setExternalNoNamespaceSchemaLocation ) : is there a way to tell the parser to validate automatically the xml with xsd in case xml refers to a xsd ?

Can somebody help me on that ?

FH

 		 	   		  

AW: How to validate a xml file that refers to an xsd file using sax parser provided by Xerces-C++ ?

Posted by Da...@dlr.de.
Hello!

SAX2Counter.exe which is included in XERCES already performs a check against an xsd. Maybe this is a good starting point.

Sincerely,
Daniel

Von: Franck Houssen [mailto:fh_p@hotmail.com]
Gesendet: Montag, 19. März 2012 13:29
An: c-users@xerces.apache.org
Betreff: How to validate a xml file that refers to an xsd file using sax parser provided by Xerces-C++ ?

Hello,

I try without success to validate a xml file that refers to an xsd file using sax parser provided by Xerces-C++ ?

The following code does not work ( parsing a not well-formed xml will report error as expected, but an invalid xml - with respect to xsd - will not report error ) :
XMLPlatformUtils::Initialize ();
SAXParser * pParser = new SAXParser ();
if ( pParser )
{
  pParser -> setDoSchema ( true );
  pParser -> setLoadSchema ( true );
  pParser -> setExternalNoNamespaceSchemaLocation ( xsdFile.c_str () );
  pParser -> setValidationScheme ( SAXParser::Val_Auto );
  pParser -> setValidationSchemaFullChecking ( true );
  pParser -> setValidationConstraintFatal ( true );

  SaxParserHandler handler; // SaxParserHandler derives from HandlerBase
  pParser -> setDocumentHandler ( &handler );
  pParser -> setErrorHandler ( &handler );
  pParser -> parse ( xmlFile.c_str () );

  delete pParser; pParser = NULL;
}
XMLPlatformUtils::Terminate ();

I want the parse to stop if the xml is not compliant with the xsd. Bonus if I don't have to handle "by hand" the xsd ( setExternalNoNamespaceSchemaLocation ) : is there a way to tell the parser to validate automatically the xml with xsd in case xml refers to a xsd ?

Can somebody help me on that ?

FH