使用Xerces C ++在XML解析期间集成架构元数据 [英] Integrate schema metatdata during XML Parsing with Xerces C++
问题描述
当我遍历DOM时,我想解析XML文件并在XML模式文件(.xsd)中查找属性和实体的数据类型.
I want to parse an XML file and look up the datatype of attributes and entities in an XML schema file (.xsd) when I traverse the DOM.
我发现我可以使用后架构验证信息集(PSVI)来获取该信息.为此,我应该能够通过getFeature方法获取节点信息:
I found out that I can use the post schema validation infoset (PSVI) to get that information. For this I should be able to get a nodes info by the getFeature method:
info = (xercesc::DOMPSVITypeInfo*) domNode->getFeature(xercesc::XMLUni::fgXercesDOMHasPSVIInfo, xercesc::XMLUni::fgVersion1_1);
但是,我首先似乎必须启用此功能.由于解析器对象中没有setFeature方法,因此我尝试了"useImplementation",但这只会使我的程序崩溃.
However I first seem to have to enable this feature. As there is no setFeature method in the parser object I tried "useImplementation", but this just crashes my program.
由于Xerces的文档相对于PSVI而言非常稀疏,因此也许有人在这里知道如何在使用XercesDOMParser解析XML文档时获取架构信息.
As the documentation of Xerces is pretty sparse in respect to PSVI, maybe someone here knows how to get schema information while parsing an XML document using a XercesDOMParser.
提前谢谢!
推荐答案
我同时发现了一个解决方案:
I found a solution meanwhile:
//create parser
static const XMLCh gLS[] = { xercesc::chLatin_L, xercesc::chLatin_S, xercesc::chNull };
xercesc::DOMImplementation *impl = xercesc::DOMImplementationRegistry::getDOMImplementation(gLS);
DOMLSParserImpl* parser = dynamic_cast<DOMLSParserImpl*>(impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
//configure
parser->setParameter(xercesc::XMLUni::fgXercesDOMHasPSVIInfo, true); //collect schema info
parser->setParameter(xercesc::XMLUni::fgDOMComments, false); //discard comments
parser->setExternalNoNamespaceSchemaLocation("schema.xsd");
parser->setDoSchema(true);
parser->setDoNamespaces(true);
parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
parser->parseURI("file.xml");
...
xercesc::DOMAttr& attr = (xercesc::DOMAttr&) attributeNode;
cout << " name: " << transcode(attr.getName()) << " type: " << transcode(attr.getSchemaTypeInfo()->getTypeName()) << ", ";
将解析器转换为Impl类有点混乱,但这是我发现访问setParameter函数的唯一方法.我认为必须有一种正确"的方法来初始化解析器,但是...
It's a bit messy to cast the the parser down to the Impl class, but it's the only way I found to access the setParameter function. I think there must be a "correct" way to initialize the parser, though...
同样重要的是,设置验证方案并将DoSchema设置为true,否则解析器将不会将架构信息附加到DOM元素上.
Also it's important to set the validation scheme and set DoSchema to true, otherwise the parser won't attach the schema information the the DOM elements.
这篇关于使用Xerces C ++在XML解析期间集成架构元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!