With the help of Philippe and a DOM sample I constructed the program shown below.  I find the exception stratgy usefully to detect really exceptional conditions as stated in the API doc, for example - a malformed xml header.
 
I am still struggling however to understand how to detect logical problems with the content of the xml document as defined by its schema around parse time.
 
Thanks,
Kevin
 
#include <xercesc\parsers\XercesDOMParser.hpp>
#include <xercesc\dom\DOM.hpp>
#include <xercesc\sax\HandlerBase.hpp>
#include <xercesc\util\XMLString.hpp>
#include <xercesc\util\PlatformUtils.hpp>
#include <iostream.h>
 
int main (int argc, char* args[])
{
    try
    {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch)
    {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Error during initialization! :\n"
             << message << "\n";
        delete [] message;
        return 1;
    }
 
    XercesDOMParser* parser = new XercesDOMParser();
 
    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
    parser->setErrorHandler(errHandler);
    parser->setValidationScheme(XercesDOMParser::Val_Always);   // optional.
    parser->setValidationSchemaFullChecking(true);             
    parser->setDoNamespaces(true);                              // optional
    parser->setDoSchema(true);
    parser->setExitOnFirstFatalError(true);
    parser->setValidationConstraintFatal(true);
    parser->setIncludeIgnorableWhitespace(false);
    parser->setCreateCommentNodes(false);
    char* xmlSchema = "C:\\Schema Validation\\01_PageInformation_0205.xsd";
    parser->setExternalSchemaLocation(xmlSchema);
 
    try
    {
        char* xmlFile = "C:\\Schema Validation\\01_PageInformation_0205.xml";
        parser->parse(xmlFile);
    }
    catch (const XMLException& toCatch)
    {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"
             << message << "\n";
        delete [] message;
        return -1;
    }
    catch (const DOMException& toCatch)
    {
        char* message = XMLString::transcode(((XMLException&)toCatch).getMessage());
        cout << "Exception message is: \n"
             << message << "\n";
        delete [] message;
        return -1;
    }
    catch (...)
    {
        cout << "Unexpected Exception \n" ;
        return -1;
    }
 
    delete parser;
    delete errHandler;
    return 0;
}
-----Original Message-----
From: Philippe HAUTION [mailto:[EMAIL PROTECTED]]
Sent: 21/11/2002 10:46
To: [EMAIL PROTECTED]
Subject: Re: DOM Validation of XML against a schema

Hello,

You could define an ErrorHandler and then try something like this before parsing :

    parser->setErrorHandler(myErrorHandler);
    parser->setValidationScheme(XercesDOMParser::Val_Always);
    parser->setValidationSchemaFullChecking(true);
    parser->setDoNamespaces(true);
    parser->setDoSchema(true);
    parser->setExitOnFirstFatalError(true); // exception si le schema n'est pas trouve
    parser->setValidationConstraintFatal(true);
    parser->setIncludeIgnorableWhitespace(false);
    parser->setCreateCommentNodes(false);

    parser->setExternalSchemaLocation( ... );

We use XercesDomParser this way and get the parsing errors as exceptions.

Regards,

Kevin Belhumeur wrote:
How do I validate XML against a schema when parsing using DOM as in:

pDomDocument = pDOMBuilder->parse( domInputSource );

I have schemas for the simple XML files in the domInputSource and am mainly interested in getting useful structure/content error messages at parse time.

Thanks,
Kevin 
 
Kevin M. Belhumeur, Software Engineer
X-Rite Incorporated
[EMAIL PROTECTED]
ph: 616-257-2478
cell: 616-644-2851



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



-- 
Philippe Haution
EDF R&D
D�partement M�thodes d'Optimisation et de Simulation
1 Av du G�n�ral de Gaulle
92141 Clamart CEDEX

Reply via email to