Search code examples
xmlxpathtinyxpath

How to find child elements of a specific node ? with TinyXpath


I have successfully used TinyXpath with root node as below

const char* xpath ="/MyRoot/A/B";
TinyXpath::xpath_processor xp_proc(mRootElement, xpath);

(this will find all B under all A of MyRoot)

I wonder if I can pass non-root element to the constructor something like below

const char* xpath = "./A/B";
TinyXpath::xpath_processor xp_proc(A_Element, xpath);

(I want to find all B under specific A, when I have A_Element)

Thank you


Solution

  • Given this constructor definition from the TinyXPath documentation:

    xpath_processor (const TiXmlNode *XNp_source_tree, 
                     const char *cp_xpath_expr)
    

    You could have:

    xpath_processor(A_Element, "A/B");
    

    provided that A_Element is of type TiXmlNode*

    This will select all B elements that are children of an A element that is a child of the element referenced by A_Element.

    In case you want to select all B elements that are children of the element referenced by A_Element, then the call should be:

    xpath_processor(A_Element, "B");