Xerces-c:使用xsd文件C ++进行XML文件验证

问题描述:

我正在尝试使用Xerces-c.我有一个.xsd方案,想用它来验证XML文件.我已经定义了xsd文件和错误处理程序,但是由于某种原因,xsd不会引发错误.对我可能会缺少的东西有什么见识?

I am attempting to use Xerces-c. I have a .xsd scheme and want to use it to to validate an XML file. I've define the xsd file and an error handler, but for some reason the xsd is not throwing errors. Any insights to what I may be missing?

    XercesDOMParser* parser = new XercesDOMParser();
    parser->setExternalNoNamespaceSchemaLocation("parser.xsd");
    parser->setExitOnFirstFatalError(true);
    parser->setValidationConstraintFatal(true);
    parser->setValidationScheme(XercesDOMParser::Val_Auto);
    parser->setDoNamespaces(true);    
    parser->setDoSchema(true);      

    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
    parser->setErrorHandler(errHandler);

    char* xmlFile = "sample.xml";

    try {
         ....
    } catch (const DOMException& e) {
         cout << "Exception.." << endl;
    }

谢谢.

当您说没有看到错误时,您是什么意思?您是否希望引发异常?如果这是您的期望,则不会发生这种情况,因为您已按如下所示设置了ErrorHandler.尝试实现其中的方法以将错误输出到控制台.同时发布您的XSD和XML

What do you mean when you say you are not seeing errors? Are you expecting an exception to be thrown? If that is your expectation then this will not happen because you have set the ErrorHandler as below. Try implementing the methods in it to print errors to the console. Also post your XSD and XML

parser->setErrorHandler(errHandler);