美好的一天。问题如下。我有一个有效的 *.xml 文件,我尝试使用以下代码对其进行解析:
for(XMLSize_t i = 0; i < childrenNodeCount; ++i)
{
DOMNode* currentNode = children->item(i);
if ((currentNode->getNodeType() != 0) && (currentNode->getNodeType() == DOMNode::ELEMENT_NODE))
{
DOMElement* currentElement = dynamic_cast<xercesc::DOMElement*>(currentNode); // !!!
if (XMLString::equals(currentElement->getTagName(), TAG_SectionHeader))
{
// parse this part
}
if (XMLString::equals(currentElement->getTagName(), TAG_SectionBody))
{
// parse this part
}
}
}
程序在执行期间崩溃,并在第一次“等于”检查时出现 SIGILL。调试显示,动态转换后,currentElement 实际上是一个空指针。这可能是什么问题?
使用 xlc++、Xerces 库 2.5、AIX 7 进行编译。
附言显然,相同的代码在 Windows 上运行良好。
更新:将 dynamic_cast 更改为 static_cast 使代码运行无误。然而,它留下了一些悬而未决的问题。
1) 为什么在 Windows 上使用 dynamic_cast 而在 Unix 上却没有运行代码?可能是编译器或库版本问题?
2) 在这种情况下是否有更好/更清洁的转换方式?
最佳答案
您没有提到编译器版本或选项,您是否指定了 -qrtti 以启用 dynamic_cast?
-qrtti=<option> | -qnortti
(C++) Generates runtime type identification (RTTI)
information for the typeid and dynamic_cast
operators. The suboptions are:
all
Generates the information needed for the RTTI
typeid and dynamic_cast operators.
type | typeinfo
Generates the information needed for the RTTI
typeid operator only.
dyna | dynamiccast
Generates the information needed for the RTTI
dynamic_cast operator only.
Default: -qnortti
关于c++ - Xerces。将 DOMNode 指针动态转换为 DOMElement 返回 nullptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32116929/