Search code examples
c++xmlaixxercesxlc

Xerces. Dynamic casting DOMNode pointer to DOMElement returns a nullptr


Good day. The problem is the following. I have a valid *.xml file which I try to parse using the following code:

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
    }
  }
}

Program crashes during execution with a SIGILL on first "equals" check. Debugging showed, that after the dynamic cast, currentElement is actually a null pointer. What can be the problem here?

Compiling with xlc++, Xerces library 2.5, AIX 7.

P.S. same code works fine on Windows apparently.

UPDATE: changing dynamic_cast to static_cast made the code run without errors. However it left some questions unanswered.

1) Why did the code run without errors while using dynamic_cast on Windows but not on Unix? Could it be a compiler or a library version issue?

2) Is there a better/cleaner way to cast in this case?


Solution

  • Ok, the reason was the following: the Xerces library is not built by default with RTTI enabled. To make mechanics in the OP work, the library should be rebuilt with RTTI enabled. https://issues.apache.org/jira/browse/XERCESC-819