Question

I am trying to determine if a given XML file is valid (has proper syntax and structure), and I am using Xerces. I have been able to succesfully read proper files but when I give it files with incorrect syntax, no errors are thrown.

I have been fishing around and found out that I might have to use an Error handler and user setErrorHandler to catch the errors instead of the traditional try-throw-catch exception handling.

The problem that I am having though is that I am very confused how to declare the proper handler, set it to my parser and then read the errors if there are any that show up.

Is there any chance somebody could shed some light on my situation?

// @input_parameter from function: const string & xmlConfigArg 

xercesc::DOMDocument* doc = NULL;

string xmlConfig(xmlConfigArg);

 Handler handler; // I'm not sure what type of handler to use
_parser->setErrorHandler(&handler);
try{
    _parser->parse(xmlConfigArg.c_str());
doc = _parser-> getDocument();
}catch(...){
    //Nothing is ever caught here
}
Was it helpful?

Solution

You need to derive a class from ErrorHandler (< xercesc/sax/ErrorHandler.hpp >) then overwrite all the virtual methods there. After doing so, You can get the error code from the class you created. No exceptions will be thrown in the parsing, so you can wave the try/cache block (or keep it for a different use).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top