Searching a DOM Tree using XPath

This topic explains how to search in a DOM tree using an XPath expression.


XPath is a simple query language for XML. An XPath expression describes a subset of an XML tree, such as all the elements with a specified name, or all the children of a specific node. Evaluating an XPath expression means searching in the XML tree for matching nodes or content: clients of the XML Engine use it to explore XML data.

The following steps show how to make an XPath query with the XML Engine.


Before you start, you must:

  • understand XPath syntax and the concept of Document Object Model (DOM).

  • understand the architecture and classes of the XML Engine component.

  • have created an [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]RXmlEngDocument (called myDoc in this example), either by parsing an XML file (as indicated in the XML DOM Parsing Tutorial) or by adding nodes to a new DOM tree.


  1. Create an instance of [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]RXmlEngXPathExpression by calling the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngXpathEvaluator::CreateExpressionL() method.

    RXmlEngXPathExpression expr = TXmlEngXpathEvaluator::CreateExpressionL( "@*" );
    This XPath expression is only useful to illustrate this tutorial: it returns all the attributes in an XML tree.
  2. Evaluate the XPath expression by applying it to your XML document.
    RXmlEngXPathResult searchResult = expr.EvaluateL( myDoc );
    The searchResult variable contains a tree of [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngAttr objects. This tree is a subset of the DOM tree.
  3. Use the result, for example by accessing it as a node set as follows:
    RXMLEngNodeSet set = result.AsNodeSet();
    if( for TInt i = 0; i < set.Length(); i++ )
        TXmlEngNode resultNode = set[i];
        /* [...] do something with the result */
  4. Free your resources as necessary.

Related tasks