【问题标题】:Validating XML that doesn't define a schema using Xerces使用 Xerces 验证未定义模式的 XML
【发布时间】:2010-12-07 09:45:20
【问题描述】:

我正在使用 Xerces-C++(2.6.1 版)SAX2 解析器来验证 XML,例如下面的文档。 (这是 MSML - RFC 5707 中定义的媒体服务器标记语言。)

<?xml version="1.0" encoding="UTF-8"?>
<msml version="1.1">
   <createconference name="example">
      <audiomix>
         <n-loudest n="3"/>
         <asn ri="10s"/>
      </audiomix>
   </createconference>
</msml>

RFC 提供了XML schemas for validating MSML,我尝试将它们与 Xerces SAX2 解析器结合使用来验证和解析 MSML。解析工作正常,但我没有得到任何验证。我怀疑我的问题可能是因为我尝试验证的 MSML 不包含 schemaLocation 属性,但我无法控制收到的 XML - 我想使用 msml.xsd 强制验证 @987654325 XML 中提供了@ 或 noNamespaceSchemaLocation(或什么都不提供)。

我的代码与下面类似。

SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();

// Enable the parser's schema support 
parser->setFeature(XMLUni::fgXercesSchema, true);

// Schema validation requires namespace processing to be turned on.
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);

// Define the location of the MSML schema.
XMLCh* schemaLocation = XMLString::transcode("/directory/path/msml-core.xsd");
parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
                    schemaLocation);

// MSMLHandler is defined elsewhere and inherits from xercesc/sax2/DefaultHandler
// It overrides startElement and fatalError.
MxMSMLHandler* msmlHandler = new MSMLHandler(xiSessionID, xoMSMLResponse);
parser->setContentHandler((ContentHandler*) msmlHandler);
parser->setErrorHandler((ErrorHandler*) msmlHandler);

// Do the parse
parser->parse(*xmlInputSource);

【问题讨论】:

    标签: c++ xml xsd xerces


    【解决方案1】:

    经过多次反复试验和错误,我最终找到了问题所在。验证错误将报告给传递给解析器的ErrorHandler 上的error 回调。 schemaLocation 属性没有问题。

    修复了这个问题,并添加了 XML 语法的缓存以提高性能,现在代码如下。

    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
    
    // Enable the parser's schema support 
    parser->setFeature(XMLUni::fgXercesSchema, true);
    
    // Schema validation requires namespace processing to be turned on.
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
    
    // Cache the XML grammar and use it for subsequent parses.
    mParser->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true);
    mParser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
    
    // Define the location of the MSML schema.
    XMLCh* schemaLocation = XMLString::transcode("/directory/path/msml-core.xsd");
    parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
                        schemaLocation);
    
    // MSMLHandler is defined elsewhere and inherits from xercesc/sax2/DefaultHandler
    // It overrides startElement, fatalError *and error*.
    MxMSMLHandler* msmlHandler = new MSMLHandler(xiSessionID, xoMSMLResponse);
    parser->setContentHandler((ContentHandler*) msmlHandler);
    parser->setErrorHandler((ErrorHandler*) msmlHandler);
    
    // Do the parse
    parser->parse(*xmlInputSource);
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2012-08-24
      • 2013-09-02
      • 2010-09-06
      • 2019-02-15
      相关资源
      最近更新 更多