Xerces的。动态转换指向DOMElement的DOMNode指针返回nullptr

时间:2015-08-20 11:23:13

标签: 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进行编译。

P.S。相同的代码显然在Windows上正常工作。

更新:将dynamic_cast更改为static_cast使代码运行时没有错误。然而,它留下了一些未回答的问题。

1)为什么在Windows上使用dynamic_cast而不是在Unix上运行时代码运行没有错误?可能是编译器还是库版本问题?

2)在这种情况下是否有更好/更清洁的投射方式?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

好的,原因如下:默认情况下,在启用RTTI的情况下,不会构建Xerces库。要使OP中的机制工作,应该在启用RTTI的情况下重建库。 https://issues.apache.org/jira/browse/XERCESC-819

相关问题