Xerces C++:不存在的文件没有错误

发布于 2024-09-10 06:21:05 字数 2404 浏览 4 评论 0原文

我正在使用 Xerces C++ DOM 解析器读取 Visual C++ 项目中的一些 XML 文件。我有一个带有 parse() 方法的类,该方法应该读取并验证我的 XML 源文件。该方法如下所示:

#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;
}

文档 < code>LocalFileInputSource 表示如果路径未解析为文件,构造函数将抛出 XMLException。但是,我可以使用任意字符串调用此方法,并且它会执行到最后,而不会引发任何异常。我做错了什么?

另外,XercesDOMParser 的 文档: :parse() 表示 SAXException 是它可以抛出的异常类型之一。我觉得这很令人困惑,因为据我了解,DOM 和 SAX 解析器是两种不同的动物,那么为什么 DOM 解析器会抛出 SAX 异常呢?

I'm using the Xerces C++ DOM parser to read some XML files in a Visual C++ project. I have a class with a parse() method that is supposed to read and validate my XML source 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 LocalFileInputSource says the constructor will throw an XMLException if the path doesn't resolve to a file. However, I can call this method with any arbitrary string and it executes to the end without any exceptions being raised. What am I doing wrong?

Also, the documentation for XercesDOMParser::parse() says a SAXException is one of the types of exceptions that it can throw. I find this confusing because from what I understand DOM and SAX parsers are 2 different animals, so why would the DOM parser throw a SAX exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兔小萌 2024-09-17 06:21:05

请参阅 ErrorHandler 文档。

您必须声明并定义一个继承自 ErrorHandler 并实现其虚拟方法的类(或者您可以扩展 HandlerBase 类)。

然后你必须调用 setErrorHandler 在解析器实例上传递错误处理程序的实例,即 pDOMParser_->setErrorHandler(your_handler_instance)

Xerces-C++ 主干样本的用法示例: SAXPrint.cpp 的第 231-233 行

更新:下面是自定义错误处理程序的示例。

#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);
        }
};

See ErrorHandler documentation.

You must declare and define a class that inherits from ErrorHandler and implements its virtual methods (or you can extend the HandlerBase class).

Then you must call setErrorHandler on your parser instance passing an instance of your error handler, i.e. pDOMParser_->setErrorHandler(your_handler_instance).

Example usage from Xerces-C++ trunk samples: rows 231-233 of SAXPrint.cpp.

Update: example of custom error handler below.

#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);
        }
};
三寸金莲 2024-09-17 06:21:05

我不认为文档说的是你认为它会做什么,它只是说它会做;扔:

XMLException 如果路径是相对路径
并且没有正确解决
文件。

如果您选择接受的话,您的任务就是找出“亲戚”的含义。恐怕我已经很多年没有使用 Xerces 了(尽管它非常强大)——我更喜欢使用小型、简单的 SAX 解析器来构建我自己的模型,而不是使用 DOM,并且不记得文件名的内容是如何工作的。

我认为您可能会遇到 SAX 异常的原因是 Xerces 使用 SAX 来构建其 DOM。

I don't think that the documentation says what you think it does, it says it will; throw:

XMLException If the path is relative
and doesn't properly resolve to a
file.

Your task, should you choose to accept it, is to find out what "relative" means. I'm afraid I I haven't used Xerces for years (though it is quite competent) - I prefer to use small, simple SAX parsers to build my own models rather than use a DOM, and can't remember how the filename stuff works.

And I think that the reason that you might get SAX exceptions is that Xerces uses SAX to build its DOM.

若无相欠,怎会相见 2024-09-17 06:21:05

2.8 Doc(您已链接)说,

XMLException 如果路径是相对且未正确解析为文件

您实际上使用的是相对路径吗?
也许对于某些特定于平台的情况曾经是这种情况,但我看不到 Xercese 2.7 中哪里提出了这种情况(我碰巧有的代码)。

查看 LocalFileFormatTarget 它可以引发“CouldNotOpenFile”异常,但没有记录为引发异常。

您使用什么版本的 xerces?

打开文件进行读取/解析看起来可能会引发“CouldNotReadFromFile”类型的丢失文件的异常。但这可能会赶上 Vanni 所说的错误处理。

The 2.8 Doc (you have linked) says,

XMLException If the path is relative and doesn't properly resolve to a file

are you actually using a relative path?
maybe this used to be the case for some platform specific cases, but I can't see where this is raised in Xercese 2.7 (code I happen to have).

Looking at LocalFileFormatTarget it can throw an exception for 'CouldNotOpenFile', but it isn't documented as raising an exception.

What version of xerces are you using?

Opening the file for reading/parsing looks like it might raise an exception for the missing file of type 'CouldNotReadFromFile'. But that could be caught up with the error handling as Vanni is talking about.

誰ツ都不明白 2024-09-17 06:21:05

我知道这已经很旧了,但是确实,我发现如果找不到文件,XercesDOMParser 会抛出 SAXParseException 。不需要自定义错误处理程序,只需捕获该异常即可。

I know this is old, but yes indeed I found that XercesDOMParser throws a SAXParseException if the file is not found. No custom error handler needed, just catch that exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文