【发布时间】:2014-07-20 14:58:23
【问题描述】:
我使用SAX2XMLReader 解析一个 XML 文件,该文件通过在其根元素中包含属性 xsi:schemaLocation 来指定 XSD 架构。
如何检索该属性的字符串值?
我试过了
parser->getProperty(XMLUni::fgXercesSchemaExternalSchemaLocation)
但它返回一个nullptr。
【问题讨论】:
我使用SAX2XMLReader 解析一个 XML 文件,该文件通过在其根元素中包含属性 xsi:schemaLocation 来指定 XSD 架构。
如何检索该属性的字符串值?
我试过了
parser->getProperty(XMLUni::fgXercesSchemaExternalSchemaLocation)
但它返回一个nullptr。
【问题讨论】:
您可以创建一个继承自 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);
}
}
}
}
【讨论】: