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 David Lowndes <da...@boldonjames.com> on 2009/03/27 11:59:20 UTC

Schema validation with V3

Hi,

I'm trying to get schema validation to work when we load our XML file.

Our code is currently like this:

     static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
     DOMImplementation *impl = 
DOMImplementationRegistry::getDOMImplementation(gLS);
     m_parser = 
((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
XMLUni::fgDOMXMLSchemaType );

	try
	{
		m_parser->resetDocumentPool();
		
		DOMConfiguration* dc = m_parser->getDomConfig();
		dc->setParameter(XMLUni::fgDOMValidate, true);
		dc->setParameter(XMLUni::fgDOMNamespaces, true);
		dc->setParameter(XMLUni::fgXercesSchema, true);
		dc->setParameter(XMLUni::fgDOMValidateIfSchema, true);
		
		m_XMLdoc = m_parser->parseURI( PathToXMLFile );
...

Our xml file has the following:

<MFGConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
version="1" xsi:noNamespaceSchemaLocation="MFGConfig.xsd">

.. and the xsd file is in the same location as the XML file.

However we're not getting seeing any exceptions due to schema validation 
errors.

Presumably we're doing something fundamentally wrong - but what?

David Lowndes


Re: Schema validation with V3

Posted by David Lowndes <da...@boldonjames.com>.
> The parser never throws exceptions to report errors.  Take a look at the 
> DOMPrint sample to see how you can install a custom DOMErrorHandler 
> instance to report errors.

Thanks Dave, that's got me going.

For the benefit of others, the changes to my code were:

1. The addition of a derived DOMErrorHandler class:

class MyDOMErrorHandler : public DOMErrorHandler
{
public:

     MyDOMErrorHandler(){};
     ~MyDOMErrorHandler(){};

	// Records the error messages
	vector<string> m_sErrs;

     bool handleError(const DOMError& domError)
	{
		string sErr;
		
		switch ( domError.getSeverity() )
		{
		case DOMError::DOM_SEVERITY_WARNING:
			sErr = "Warning: ";
			break;
		
		case DOMError::DOM_SEVERITY_ERROR:
			sErr = "Error: ";
			break;
		
		case DOMError::DOM_SEVERITY_FATAL_ERROR:
			sErr = "Fatal: ";
			break;
			
		default:
			sErr = "Unknown: ";
			break;
		}

		char *msg = XMLString::transcode(domError.getMessage());
		sErr += msg;
		XMLString::release(&msg);
		
		m_sErrs.push_back( sErr );

		/* Returning false here doesn't cause parseURI to return NULL,
		 * but it does prevent further schema errors being recorded,
		 * so return true as though nothing is wrong and check the number of
		 * recorded errors from here as evidence of schema validation failing.
		 */
		return true;
	}
	
     void resetErrors(){};

private :
     /* Unimplemented constructors and operators */
     MyDOMErrorHandler(const DOMErrorHandler&);
     void operator=(const DOMErrorHandler&);
};


2. Set the error handler before calling parseURI and check for any 
recorded errors from the error handler:

	MyDOMErrorHandler myErrorHandler;
	dc->setParameter(XMLUni::fgDOMErrorHandler, &myErrorHandler);
		
	m_XMLdoc = m_parser->parseURI( MyXMLFileName );
	if (m_XMLdoc == NULL)
	{
		...
	}
		
	if ( myErrorHandler.m_sErrs.size() != 0 )
	{
		... log the errors recorded
	}


Re: Schema validation with V3

Posted by David Bertoni <db...@apache.org>.
David Lowndes wrote:
> Hi,
> 
> I'm trying to get schema validation to work when we load our XML file.
> 
> Our code is currently like this:
> 
>     static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
>     DOMImplementation *impl = 
> DOMImplementationRegistry::getDOMImplementation(gLS);
>     m_parser = 
> ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
> XMLUni::fgDOMXMLSchemaType );
> 
>     try
>     {
>         m_parser->resetDocumentPool();
>        
>         DOMConfiguration* dc = m_parser->getDomConfig();
>         dc->setParameter(XMLUni::fgDOMValidate, true);
>         dc->setParameter(XMLUni::fgDOMNamespaces, true);
>         dc->setParameter(XMLUni::fgXercesSchema, true);
>         dc->setParameter(XMLUni::fgDOMValidateIfSchema, true);
>        
>         m_XMLdoc = m_parser->parseURI( PathToXMLFile );
> ...
> 
> Our xml file has the following:
> 
> <MFGConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> version="1" xsi:noNamespaceSchemaLocation="MFGConfig.xsd">
> 
> .. and the xsd file is in the same location as the XML file.
> 
> However we're not getting seeing any exceptions due to schema validation 
> errors.
The parser never throws exceptions to report errors.  Take a look at the 
DOMPrint sample to see how you can install a custom DOMErrorHandler 
instance to report errors.

Dave