Christian,
to define my own DOMErrorHandler class I use the following #includes in
the header file:
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/DOMLocator.hpp>
XERCES_CPP_NAMESPACE_USE
This code snippet should do the validation for you. Please note that I
don't set the schema instance here. So you might want to add this on
your own:
// Initialize the XML4C system
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
strGeneralResult.Format(_T("Error during Xerces
initialization! :\n%s\n"), toCatch.getMessage());
return;
}
// Instantiate the DOM parser.
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(gL S);
DOMBuilder* parser = ((DOMImplementationLS
*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHR ONOUS, 0);
parser->setFeature(XMLUni::fgDOMNamespaces, true);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
parser->setFeature(XMLUni::fgDOMValidation, true);
// enable datatype normalization - default is off
parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);
// And create our error handler and install it
// CMyDOMErrorHandler must be derived from DOMErrorHandler
parser->setErrorHandler(new CMyDOMErrorHandler);
//
// Get the starting time and kick off the parse of the indicated
// file. Catch any exceptions that might propogate out of it.
//
unsigned long duration;
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* doc = 0;
try
{
// reset document pool
parser->resetDocumentPool();
doc = parser->parseURI(_T("C:\your_file.xml"));
}
catch (const XMLException& toCatch)
{
strGeneralResult.Format(_T("Error during parsing: %s\nXML
Exception message is:\n %s\n"), strXMLFile, toCatch.getMessage());
}
catch (const DOMException& toCatch)
{
const unsigned int maxChars = 2047;
XMLCh errText[maxChars + 1];
strGeneralResult.Format(_T("DOM Error during parsing:
%s\nDOMException code is: %d\n"), strXMLFile, toCatch.code);
if (DOMImplementation::loadDOMExceptionMsg(toCatch.co de,
errText, maxChars))
{
CString strDOMExc;
strDOMExc.Format(_T("\nMessage is: %s"), errText);
strGeneralResult +=strDOMExc;
}
}
catch (...)
{
strGeneralResult.Format(_T("Unexpected exception during
parsing: %s\n"), strXMLFile);
}
Regards
spiff wrote:
Christian,
I'm sorry but I don't have the code available now but I will post it
this (late) evening (european time).
I'm not aware of any other method to know if the XML is valid then
taking a look at the DOMErrorHandler exceptions. If you didn't get one
the XML is valid. And I also don't know any other method to set the
error handler.
The schema could also assigned in the XML instance itself. In fact I
believe this is the more common case.
Maybe try to get it running with a real DOMErrorHandler in the
meantime.
spiff
http://www.spycomponents.com
ValidatorBuddy - A tool for using different XML validators
ch****************@nt.uni-saarland.de schrieb:
Hello spiff,
thanks A LOT for your fast response. First of all, I should mention
that I am a totally newbie in Xerces so that i most probably did a
quite elementary mistake.
What do you mean with nothing happens?
In terms of nothing happens means, when i manipulate either the schema
file or the xml file in a way that they become inconsistently, I still
dont get any Exception call. I thought that if the validation process
fails, the parsing method throws an DOMException which I can catch with
the follwing section... ?
catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}
Is ErrorHandler of type DOMErrorHandler?
The Error Handler itself is not of type DOMErrorHandler cause when I
want to parse an DOMErrorHandler to the setErrorHandler() function, I
get an error message roughly saying:
(translated..) Error: no suitable function call for
xercesc_2_7::XercesDOMParser::setErrorHandler(xerc esc_2_7::DOMErrorHandler*&)«
.. even though I have included <xercesc/dom/DOMErrorHandler.hpp>... Is
there another way of assigning this ErrorHandler to the parser?? By the
way, is it mandatory to instantiate my own error handler methods to
just indicate a validation problem? I dont necessarily need to know
where the error as happened nor what error. I just want to know VALID
or NOT VALID..
>What happens if you remove the setExternalNoNamespaceSchemaLocation() statement?
In terms of the setExternalNoNamespaceSchemaLocation() statement I just
wanted to assign the pathname of my external .xsd file
("/home/eickhoff/myfile.xsd"). What other way could I make use of to
tell the parser where to find my external schema? As I finally seem to
have made a more trivial mistake, this function has no influence on the
performance at all -means if I erase it, the behaviour of the code is
still the same.
Other solutions than mine to easily validate an XML file against its
external Schema are highly appreciated as well!!
Best regards,
Christian A. Eickhoff