【问题标题】:How to retrieve xsi:schemaLocation with xercesc::SAX2XMLReader如何使用 xercesc::SAX2XMLReader 检索 xsi:schemaLocation
【发布时间】:2014-07-20 14:58:23
【问题描述】:

我使用SAX2XMLReader 解析一个 XML 文件,该文件通过在其根元素中包含属性 xsi:schemaLocation 来指定 XSD 架构。

如何检索该属性的字符串值?

我试过了

parser->getProperty(XMLUni::fgXercesSchemaExternalSchemaLocation)

但它返回一个nullptr

【问题讨论】:

    标签: c++ xml xsd xerces-c


    【解决方案1】:

    您可以创建一个继承自 PSVIHandler 的类 MyPSVIHandler 并使用 setPSVIHandler 方法安装它。

    MyPSVIHandler psvi_handler;
    sax2xmlreader->setPSVIHandler(&psvi_handler);
    
    void MyPSVIHandler::handleAttributesPSVI(
        const XMLCh *const /* localName */,
        const XMLCh *const /* uri */,
        PSVIAttributeList *psviAttributes) {
      for (XMLSize_t i = 0; i < psviAttributes->getLength(); i++) {
        if (XMLString::equals(psviAttributes->getAttributeNamespaceAtIndex(i),
                              SchemaSymbols::fgURI_XSI)) {
          const XMLCh *attr_name(psviAttributes->getAttributeNameAtIndex(i));
          if (XMLString::equals(attr_name, SchemaSymbols::fgXSI_SCHEMALOCATION)) {
            const XMLCh *str = psviAttributes->
                getAttributePSVIAtIndex(i)->getSchemaNormalizedValue();
            char *transcoded_str = xercesc::XMLString::transcode(str);
            std::cout << "schemaLocation = " << transcoded_str << "\n";
            xercesc::XMLString::release(&transcoded_str);
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      相关资源
      最近更新 更多