Xerces C ++: no error for nonexistent file

I am using the Xerces C ++ DOM parser to read some XML files in a Visual C ++ project. I have a class with a method parse()that needs to read and validate the source XML file. This is what the method looks like:

#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>           
#include <xercesc/parsers/XercesDOMParser.hpp>  
#include <xercesc/framework/LocalFileInputSource.hpp>

using namespace std;
XERCES_CPP_NAMESPACE_USE

unsigned long RulesParser::parse( const wstring &xmlFile )
{
  if( parserInitialized_ == false ) {
    try {
      XMLPlatformUtils::Initialize();   /* initialize xerces */
    } catch( XMLException const &e ) {
      return Status::PARSER_INIT_FAIL;
    }
  }
  parserInitialized_  = true;           /* indicate xerces has been 
                                           successfully initialized */

  if( pDOMParser_ != NULL ) {
    delete pDOMParser_;
  }
  pDOMParser_ = new XercesDOMParser;    /* create a DOM parser instance */
  /* set xerces options */
  pDOMParser_->setDoNamespaces( true ); /* enable namespace processing */
  pDOMParser_->setDoSchema( true );     /* enable schema processing */
  pDOMParser_->setValidationScheme( XercesDOMParser::Val_Always );  /* parser always validates */
  pDOMParser_->setValidationSchemaFullChecking( true ); /* enable full schema checking */

  auto_ptr< LocalFileInputSource > srcFile; /* XML source file loader */

  try {
    srcFile.reset( new LocalFileInputSource( xmlFile.c_str() ) );

  } catch( const XMLException &e ) {
    return Status::XML_SOURCE_LOAD_ERROR;
  }

  /* parse the file */
  try {
    pDOMParser_->parse( *srcFile );

  } catch( const XMLException &e ) {    
    return Status::XML_SOURCE_PARSE_ERROR;

  } catch( const DOMException &e ) {
    return Status::XML_SOURCE_PARSE_DOM_ERROR;
  }

  return Status::OK;
}

The documentation for LocalFileInputSourcesays that the constructor will throw XMLExceptionif the path is not allowed for the file. However, I can call this method with any arbitrary string, and it runs to the end without any exceptions. What am I doing wrong?

, XercesDOMParser::parse() , SAXException - , . , , , DOM SAX - , DOM- SAX?

+3
4

. ErrorHandler .

, ErrorHandler ( HandlerBase ).

setErrorHandler , , pDOMParser_->setErrorHandler(your_handler_instance).

Xerces-++: 231-233 SAXPrint.cpp.

.

#include <iostream>
#include <xercesc/sax/HandlerBase.hpp>
XERCES_CPP_NAMESPACE_USE

class CustomErrorHandler : public HandlerBase
{

    public:

        CustomErrorHandler() {}

        void error(const SAXParseException& e)
        {
            handler(e);
        }

        void fatalError(const SAXParseException& e)
        {
            handler(e);
        }

        void warning(const SAXParseException& e)
        {
            handler(e);
        }

    private:

        void handler(const SAXParseException& e)
        {
            char* message = XMLString::transcode(e.getMessage());

            cerr << "line " << e.getLineNumber()
                 << ", column " << e.getColumnNumber()
                 << " -> " << message << "\n\n";

            XMLString::release(&message);
        }
};
+4

, , , , , ; :

XMLException .

, , - , "". , Xerces ( ). SAX , DOM , .

, , SAX, , Xerces SAX DOM.

0

2.8 Doc ( ) :

XMLException

? , , , Xercese 2.7 (, ).

LocalFileFormatTarget, 'CouldNotOpenFile', .

xerces ?

/ , "CouldNotReadFromFile". , .

0

I know this is old, but yes, indeed, I found that it XercesDOMParserthrows SAXParseExceptionif the file is not found. No custom error handler is needed, just catch this exception.

0
source

Source: https://habr.com/ru/post/1754320/


All Articles