Xerces-C XML 模式验证不起作用
尝试让 Xerces-C 根据架构文件验证 XML 文件,但没有成功。下面的构造函数接受 XML 文件和架构文件的位置,并设置相关成员变量:
Config::Config(const std::string& schemaFile, const std::string& XMLFile)
: m_schemaFile(schemaFile),
m_XMLFile(XMLFile)
{
{
//initialize
try
{
xercesc::XMLPlatformUtils::Initialize();
xalanc::XPathEvaluator::initialize();
}
catch (xercesc::XMLException& e)
{
throw XercesInitialisationException();
}
}
{
//validate XML
xercesc::XercesDOMParser m_domParser;
if (NULL == m_domParser.loadGrammar(m_schemaFile.c_str(), xercesc::Grammar::SchemaGrammarType))
{
//schema file could not be loaded
throw SchemaLoadException();
}
ParserErrorHandler errorHandler;
m_domParser.setErrorHandler(&errorHandler);
m_domParser.setDoNamespaces(true);
m_domParser.setDoSchema(true);
m_domParser.setValidationConstraintFatal(true);
m_domParser.setValidationSchemaFullChecking(true);
m_domParser.parse(m_XMLFile.c_str());
if (NULL == m_domParser.getDocument() || NULL == m_domParser.getDocument()->getDocumentElement())
{
throw XMLLoadException();
}
if (0 == m_domParser.getErrorCount())
{
std::cout << "Number of schema validation errors: " << m_domParser.getErrorCount() << std::endl;
}
else
{
//m_validated unsuccessfully against the schema
throw SchemaValidationException();
}
}
{
//set up XPath interpreter
const xalanc::XalanDOMString m_xalanXMLFile(m_XMLFile.c_str());
const xercesc::LocalFileInputSource m_xalanInputSource(m_xalanXMLFile.c_str());
// Initialise XalanSourceTree subsystem...
xalanc::XalanSourceTreeInit sourceTreeInit;
m_liaison = std::auto_ptr<xalanc::XalanSourceTreeParserLiaison>(new xalanc::XalanSourceTreeParserLiaison(m_domSupport));
m_domSupport.setParserLiaison(m_liaison.get());
m_document = m_liaison->parseXMLStream(m_xalanInputSource);
m_prefixResolver = std::auto_ptr<xalanc::XalanDocumentPrefixResolver>(new xalanc::XalanDocumentPrefixResolver(m_document));
m_evaluator = std::auto_ptr<xalanc::XPathEvaluator>(new xalanc::XPathEvaluator);
}
}
设置 XML 解析的构造函数区域如下所示。我认为这就是问题所在:
//validate XML
xercesc::XercesDOMParser m_domParser;
if (NULL == m_domParser.loadGrammar(m_schemaFile.c_str(), xercesc::Grammar::SchemaGrammarType))
{
//schema file could not be loaded
throw SchemaLoadException();
}
ParserErrorHandler errorHandler;
m_domParser.setErrorHandler(&errorHandler);
m_domParser.setDoNamespaces(true);
m_domParser.setDoSchema(true);
m_domParser.setValidationConstraintFatal(true);
m_domParser.setValidationSchemaFullChecking(true);
m_domParser.parse(m_XMLFile.c_str());
if (NULL == m_domParser.getDocument() || NULL == m_domParser.getDocument()->getDocumentElement())
{
throw XMLLoadException();
}
if (0 == m_domParser.getErrorCount())
{
std::cout << "Number of schema validation errors: " << m_domParser.getErrorCount() << std::endl;
}
else
{
//m_validated unsuccessfully against the schema
throw SchemaValidationException();
}
当代码编译并运行时,一切正常,但没有针对架构对 XML 进行验证。这意味着即使 XML 不符合架构,也会对其进行解析。经过检查,我还能够确定所有模式的 errorCount 值均保持为 0。
Trying to get Xerces-C to validate an XML file against a schema file but with no luck. The constructor below takes in the location of the XML file and the schema file and sets relevent member variables:
Config::Config(const std::string& schemaFile, const std::string& XMLFile)
: m_schemaFile(schemaFile),
m_XMLFile(XMLFile)
{
{
//initialize
try
{
xercesc::XMLPlatformUtils::Initialize();
xalanc::XPathEvaluator::initialize();
}
catch (xercesc::XMLException& e)
{
throw XercesInitialisationException();
}
}
{
//validate XML
xercesc::XercesDOMParser m_domParser;
if (NULL == m_domParser.loadGrammar(m_schemaFile.c_str(), xercesc::Grammar::SchemaGrammarType))
{
//schema file could not be loaded
throw SchemaLoadException();
}
ParserErrorHandler errorHandler;
m_domParser.setErrorHandler(&errorHandler);
m_domParser.setDoNamespaces(true);
m_domParser.setDoSchema(true);
m_domParser.setValidationConstraintFatal(true);
m_domParser.setValidationSchemaFullChecking(true);
m_domParser.parse(m_XMLFile.c_str());
if (NULL == m_domParser.getDocument() || NULL == m_domParser.getDocument()->getDocumentElement())
{
throw XMLLoadException();
}
if (0 == m_domParser.getErrorCount())
{
std::cout << "Number of schema validation errors: " << m_domParser.getErrorCount() << std::endl;
}
else
{
//m_validated unsuccessfully against the schema
throw SchemaValidationException();
}
}
{
//set up XPath interpreter
const xalanc::XalanDOMString m_xalanXMLFile(m_XMLFile.c_str());
const xercesc::LocalFileInputSource m_xalanInputSource(m_xalanXMLFile.c_str());
// Initialise XalanSourceTree subsystem...
xalanc::XalanSourceTreeInit sourceTreeInit;
m_liaison = std::auto_ptr<xalanc::XalanSourceTreeParserLiaison>(new xalanc::XalanSourceTreeParserLiaison(m_domSupport));
m_domSupport.setParserLiaison(m_liaison.get());
m_document = m_liaison->parseXMLStream(m_xalanInputSource);
m_prefixResolver = std::auto_ptr<xalanc::XalanDocumentPrefixResolver>(new xalanc::XalanDocumentPrefixResolver(m_document));
m_evaluator = std::auto_ptr<xalanc::XPathEvaluator>(new xalanc::XPathEvaluator);
}
}
The area of the constructor where the XML parse is set up is shown below. This is where I think the problem lies:
//validate XML
xercesc::XercesDOMParser m_domParser;
if (NULL == m_domParser.loadGrammar(m_schemaFile.c_str(), xercesc::Grammar::SchemaGrammarType))
{
//schema file could not be loaded
throw SchemaLoadException();
}
ParserErrorHandler errorHandler;
m_domParser.setErrorHandler(&errorHandler);
m_domParser.setDoNamespaces(true);
m_domParser.setDoSchema(true);
m_domParser.setValidationConstraintFatal(true);
m_domParser.setValidationSchemaFullChecking(true);
m_domParser.parse(m_XMLFile.c_str());
if (NULL == m_domParser.getDocument() || NULL == m_domParser.getDocument()->getDocumentElement())
{
throw XMLLoadException();
}
if (0 == m_domParser.getErrorCount())
{
std::cout << "Number of schema validation errors: " << m_domParser.getErrorCount() << std::endl;
}
else
{
//m_validated unsuccessfully against the schema
throw SchemaValidationException();
}
When the code is compiled and ran everything works but no validation is carried out on the XML against the schema. Which means the XML is parsed even if it does not conform to the schema. After checking I have also been able to ascertain that the errorCount value remains 0 for all schemas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须像这样引用 xml 根节点上的架构:
You must to reference the schema on xml root node like this: