David Bertoni wrote:
Tim Cramer wrote:
Hello everyone,
i have got a problem with the validation with a schema. I am using
the DOMBuilder, which has a DOMErrorHandler. When I try parsing a
invalid document, it is only parsed up to the wrong element, value,
etc without any error message / exception. How can I handle a schema
error? I set the features the following way:
DOMBuilder* probParser_ =
impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS,0);
XNSQ DOMErrorHandler* errHandler = (DOMErrorHandler*) new XNSQ
HandlerBase();
This cast should not be necessary. Is there some reason it's there?
yes. I think I need a DOMErrorHandler for the DOMBuilder, but I cannot
have in instance of it, because it is abstract.
probParser_->setErrorHandler(errHandler);
probParser_->setProperty(
XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
"file.xsd" );
probParser_->setFeature( XMLUni::fgDOMNamespaces, true);
probParser_->setFeature( XMLUni::fgXercesSchema, true);
probParser_->setFeature( XMLUni::fgXercesSchemaFullChecking, true);
What happens if you set this feature:
probParser_->setFeature( XMLUni::fgDOMValidateIfSchema, true);
The same effect.
you might also try:
probParser_->setValidationScheme(AbstractDOMParser::Val_Always);
I think this method is not defined for DOMBuilder.
try{
XNSQ DOMDocument* newProblem = probParser_->parse(sourceWrapper);
; }
catch(const XMLException& toCatch){
char* message = XNSQ XMLString::transcode(toCatch.getMessage());
std::cerr << "Parsing Error.XML Exception message is:\n"
<< message << "\nPlease check your Problemfile!\n";
XNSQ XMLString::release(&message);
return false;
}
catch(const DOMException& toCatch){
char* message = XNSQ XMLString::transcode(toCatch.msg);
std::cerr << "Parsing Error. DOM Exception message is:\n"
<< message << std::endl;
XNSQ XMLString::release(&message); }
catch(const SAXException& toCatch){
std::cerr << "error\n";
}
catch(...){
;//...
}
I hope your error handler is actually throwing the exceptions it
receives. Otherwise, they will just be swallowed. Since you didn't
provide any source code for your ErrorHandler, we can only guess.
I do not have my own error handler (HandlerBase is xerces class). I
thought the DOMBuilder::parse () would through an exception if the xml
data is not conform with the schema, wouldn't it?
Another test i have made is the following:
#define XNSQ XERCES_CPP_NAMESPACE_QUALIFIER
class XMLErrorHandler : public XNSQ DefaultHandler{
public:
void warning(const XNSQ SAXParseException& e){
std::cerr<< "+WARNING+\n";
}
void error(const XNSQ SAXParseException& e){
std::cerr<< "+ERROR+\n";
char* message = XNSQ XMLString::transcode(e.getMessage());
throw std::runtime_error( message );
XNSQ XMLString::release(&message);
}
void fatalError(const XNSQ SAXParseException& e){
error(e);
}
};
But this does not solve the problem of the typecast:
XMLErrorHandler* errHandler = new XMLErrorHandler;
probParser_->setErrorHandler((XNSQ DOMErrorHandler*)( errHandler ) );
So what is the way to do it?
Or is it possible that schema support is not implemented for the
DOMBuilder (DOM Level 3) yet (the canSetFeature() methods return TRUE,
so it might be implemented...)? Thanks a lot for your help.
Tim