Searching a DOM Tree using XPath

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

Before you start, you must:

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.

  1. Create an instance of RXmlEngXPathExpression by calling the 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 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
Serialising XML DOM