Search code examples
javaxmlxpathxpath-2.0domxpath

XPath for text of node?


I'm trying to get to the content of STRING-VALUE where comparator has VALUE = item.auxiliaryData.productRef.brandName

I tried to use the following code,

XPathExpression expr = xpath.compile
                ("//comparator[contains(VALUE,'item.auxiliaryData.productRef.ancestorCategoryIds')]//STRING-VALUE");
        productRefs = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

This is what I'm trying to parse

                    <comparator NAME="equals">
                       <VALUE>item.auxiliaryData.productRef.brandName</VALUE>
                       <CONSTANT>
                          <DATA-TYPE>java.lang.STRING</DATA-TYPE>
                          <STRING-VALUE>Ariat</STRING-VALUE>
                       </CONSTANT>
                    </comparator>
                    <comparator NAME="equals">
                       <VALUE>item.auxiliaryData.productRef.ID</VALUE>
                       <CONSTANT>
                          <DATA-TYPE>java.util.Collection</DATA-TYPE>
                          <STRING-VALUE>84332</STRING-VALUE>
                          <STRING-VALUE>79904</STRING-VALUE>
                          <STRING-VALUE>82203</STRING-VALUE>
                       </CONSTANT>
                    </comparator>

Solution

  • This XPath,

    string(//comparator[VALUE='item.auxiliaryData.productRef.brandName']//STRING-VALUE)
    

    will return "Ariat" directly as requested.