Working with Xerces DOM nodes, I had the problem to query DOMNodes with X-Path. Because Xerces doesn't implement any X-Path, I tried the Pathan library. After stumpling around, I finaly tried the Xalan, to work with the Xerces DOMNodes. This is not a simple task after all. Following code should give you an idea how to do this:
//m_pNode is an instance of XERCES_CPP_NAMESPACE::DOMNode
//strQuery is the X-Path
const XALAN_CPP_NAMESPACE::XalanDOMString
strExpression(strQuery.c_str(NULL)); //UNICODE PROBLEM
XERCES_CPP_NAMESPACE::DOMDocument* pDocument =
m_pNode->getOwnerDocument();
XALAN_CPP_NAMESPACE::XercesParserLiaison mParserLiaison;
mParserLiaison.setBuildWrapperNodes(true);
mParserLiaison.setBuildMaps(true);
XALAN_CPP_NAMESPACE::XalanDocument* pXalanDocument =
mParserLiaison.createDocument(pDocument, false, false);
XALAN_CPP_NAMESPACE::XalanElement* pNameSpaceNode =
pXalanDocument->getDocumentElement();
XALAN_CPP_NAMESPACE::XercesDocumentWrapper* pXercesDocumentWrapper =
mParserLiaison.mapDocumentToWrapper(pXalanDocument);
XALAN_CPP_NAMESPACE::XalanNode* pRootContextNode =
pXercesDocumentWrapper->mapNode(m_pNode);
XALAN_CPP_NAMESPACE::XercesDOMSupport mDOMSupport;
if (pRootContextNode != NULL)
{
XALAN_CPP_NAMESPACE::XPathEvaluator mEvaluator;
XALAN_CPP_NAMESPACE::XalanNode* pFoundXalanNode =
mEvaluator.selectSingleNode(mDOMSupport,
pRootContextNode,
strExpression.c_str(),
pNameSpaceNode);
XERCES_CPP_NAMESPACE::DOMNode* pTargetNode = NULL;
if (pFoundXalanNode != NULL)
{
DOMNode const* pConstTargetNode = pXercesDocumentWrapper->mapNode(pFoundXalanNode);
pTargetNode = const_cast<DOMNode *>(pConstTargetNode);
...
}
}
Following pages helped out and have further informations:
- The pathan library is a good alternative to Xalan
http://software.decisionsoft.com/pathanIntro.html - The API documentation of Xerces C++
http://xml.apache.org/xerces-c/apiDocs/index.html - The API documentation of Xalan C++
http://xml.apache.org/xalan-c/apiDocs/index.html