c++ - Xerces。将 DOMNode 指针动态转换为 DOMElement 返回 nullptr

标签 c++ xml aix xerces xlc

美好的一天。问题如下。我有一个有效的 *.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/

相关文章:

copy - 在同一数据库DB2中创建表的副本

c++ - 如何在 C++ 中将转换应用于 STL 映射

c++ - Boggle game Implementation 无法获取所有单词

c++ - 每个运行时函数的地址是否更改

xml - 是否可以在 SuiteScript 2.0 中将图像从图像字段加载到 PDF 中

aix - 从 Linux 确定 AIX 的库存档是 32 位、64 位还是两者兼而有之

c++ - 是否有与 BUCK 的 header_namespace 等效的 cmake?

android - Problema更新和上传我的apk

c# - XmlReader 始终根据另一个 Xsd 文件验证 Xsd 文件是否有效

java - 管辖权政策文件未由受信任的签名者签名......?