Hi, I had the same problem and I solved it in this way:
--- code starts ---
XMLDOMErrorReporter* errReporter_;
XercesDOMParser* parser_;
errReporter_ = new XMLDOMErrorReporter();
parser_->setErrorHandler(errReporter_);
parser_->setValidationScheme(XercesDOMParser::Val_Always);
parser_->setDoSchema(true);
parser_->setDoNamespaces(true);
parser_->setDoValidation(true);
parser_->setValidationSchemaFullChecking(true);
parser_->setIncludeIgnorableWhitespace(false);
--- code ends ---
where the XMLDOMErrorReporter object inherits from ErrorHandler, for
example in my case:
--- code starts ---
class XMLDOMErrorReporter : public ErrorHandler {
private:
bool sawErrors;
public:
XMLDOMErrorReporter() : sawErrors(false) { }
~XMLDOMErrorReporter() { }
void
warning(const SAXParseException& e)
{
std::cerr << "-- WARNING [DOM Parser] --" << std::endl;
std::cerr << "message: " << XMLString::transcode(e.getMessage()) <<
std::endl;
std::cerr << "file: " << XMLString::transcode(e.getSystemId()) <<
std::endl;
std::cerr << "line: " << e.getLineNumber() << std::endl;
std::cerr << "column: " << e.getColumnNumber() << std::endl;
}
void
error(const SAXParseException& e)
{
sawErrors = true;
std::cerr << "-- ERROR [DOM Parser] --" << std::endl;
std::cerr << "message: " << XMLString::transcode(e.getMessage()) <<
std::endl;
std::cerr << "file: " << XMLString::transcode(e.getSystemId()) <<
std::endl;
std::cerr << "line: " << e.getLineNumber() << std::endl;
std::cerr << "column: " << e.getColumnNumber() << std::endl;
}
void
fatalError(const SAXParseException& e)
{
sawErrors = true;
std::cerr << "-- FATAL ERROR [DOM Parser] --" << std::endl;
std::cerr << "message: " << XMLString::transcode(e.getMessage()) <<
std::endl;
std::cerr << "file: " << XMLString::transcode(e.getSystemId()) <<
std::endl;
std::cerr << "line: " << e.getLineNumber() << std::endl;
std::cerr << "column: " << e.getColumnNumber() << std::endl;
}
void
resetErrors()
{
sawErrors = false;
}
bool getSawErrors() const
{
return sawErrors;
}
};
--- code ends ---
The XSD file that should be used for validation in specified in the XML
instance, for example:
--- code starts ---
<?xml version="1.0" ?>
<visor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="visor.xsd">
...
</visor>
--- code ends ---
Hope this helps!
Best regards.
Roberto
Sebastian 'CrashandDie' Lauwers wrote:
> Hi everyone,
>
> I'm trying to validate get the DOM parser to validate an XML instance
> through an XSD file, though, I'm not really sure as to how to do
> this...
>
> I've Googled quite a bit, but I think I'm looking in the wrong direction.
>
> XSD file: http://slexy.org/view/s2iGvEBQAU
> Relevant code: http://slexy.org/view/s2c2p1D8HG
>
> Could you point me in the right direction ? Any help is appreciated, really.
>
> Best regards,
>
> S.
>