我正在尝试使用 Xerces-C++ 解析 XML 文档。我只是希望能够通过它的 id 搜索一个元素。我已经编写了以下代码,但它不起作用。...
try {
XMLPlatformUtils::Initialize();
}
catch(XMLException& e) {
char* message = XMLString::transcode( e.getMessage() );
cout << "XML toolkit initialization error: " << message << endl;
XMLString::release( &message );
}
XMLCh tempStr[100];
XMLString::transcode("LS", tempStr, 99);
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMLSParser* parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
char *filename = "C:\\odx1.xml";
xercesc::DOMDocument *doc = 0;
try {
doc = parser->parseURI(filename);
DOMElement *element = doc->getElementById(XMLString::transcode("test"));
if(element != NULL) cout << "element found";
cout << "DONE";
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return;
}
catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return ;
}
parser->release();
XMLPlatformUtils::Terminate();
}
...
XML 是:
<ODX MODEL-VERSION="2.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="odx.xsd">
<DIAG-LAYER-CONTAINER ID="test">
test done
</DIAG-LAYER-CONTAINER>
</ODX>
我希望它打印“找到元素”,但程序正确终止而不打印“找到元素”。
无论如何...在与 XML 文档关联的 XSD 文件中,我正在搜索的元素有<xsd:attribute name="ID" type="xsd:ID" use="required"/>
所以我希望 getElementById 返回该元素。