C++ Blog - Using XML in C++
This listing includes all about XML in C++.
|
| 16.06.07 |
X-Path with Xerces and Xalan |
|
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:
|
|
| 16.06.07 |
MSXML: X-Path differences to Xerces C++ |
|
The MSXML Parser from Microsoft has one big difference to the XML Paser from Apache. Quering a XML node by XPath using the index-operator is different. MSXML starts at 0 (zero), the XML Parser of Xerces starts at 1 (one).
Be careful porting software from one parser to the other parser. The x-path ./MainNode/Node[1] returns on MSXML parser the 2nd child node named node.
To solve this problem I read, that the E5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
To fix this problem we have to set the SelectionLanguage property of the document to XPath.
|
|
|
Following pages helped out and have further informations:
|
|