Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xerces-c 3.1 XPath evaluation

Tags:

xpath

xerces-c

I could not find much examples of evaluate XPath using xerces-c 3.1.

Given the following sample XML input:

<abc> 
    <def>AAA BBB CCC</def>
</abc>

I need to retrieve the "AAA BBB CCC" string by the XPath "/abc/def/text()[0]".

The following code works:

XMLPlatformUtils::Initialize();
// create the DOM parser
XercesDOMParser *parser = new XercesDOMParser;
parser->setValidationScheme(XercesDOMParser::Val_Never);
parser->parse("test.xml");
// get the DOM representation
DOMDocument *doc = parser->getDocument();
// get the root element
DOMElement* root = doc->getDocumentElement();

// evaluate the xpath
DOMXPathResult* result=doc->evaluate(
    XMLString::transcode("/abc/def"), // "/abc/def/text()[0]"
    root,
    NULL,
    DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, //DOMXPathResult::ANY_UNORDERED_NODE_TYPE, //DOMXPathResult::STRING_TYPE,
    NULL);

// look into the xpart evaluate result
result->snapshotItem(0);
std::cout<<StrX(result->getNodeValue()->getFirstChild()->getNodeValue())<<std::endl;;

XMLPlatformUtils::Terminate();
return 0;

But I really hate that:

result->getNodeValue()->getFirstChild()->getNodeValue()

Has it to be a node set instead of the exact node I want?

I tried other format of XPath such as "/abc/def/text()[0]", and "DOMXPathResult::STRING_TYPE". xerces always thrown exception.

What did I do wrong?

like image 658
John Crane Avatar asked Dec 20 '25 23:12

John Crane


1 Answers

I don't code with Xerces C++ but it seems to implement the W3C DOM Level 3 so based on that I would suggest to select an element node with a path like /abc/def and then simply to access result->getNodeValue()->getTextContent() to get the contents of the element (e.g. AAA BBB CCC).

As far as I understand the DOM APIs, if you want a string value then you need to use a path like string(/abc/def) and then result->getStringValue() should do (if the evaluate method requests any type or STRING_TYPE as the result type).

Other approaches if you know you are only interested in the first node in document order you could evaluate /abc/def with FIRST_ORDERED_NODE_TYPE and then access result->getNodeValue()->getTextContent().

like image 184
Martin Honnen Avatar answered Dec 24 '25 12:12

Martin Honnen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!