Java Utililty Methods XPath Expression

List of utility methods to do XPath Expression

Description

The list of methods to do XPath Expression are organized into topic(s).

Method

XPathExpressionexpr(String xpathStr)
expr
XPath xpath = factory.newXPath();
return xpath.compile(xpathStr);
StringextractNode(XPath xpath, String nodePath, String xmlString)
Extract a String node from an XML Message
InputSource inputSource = new InputSource(new StringReader(xmlString));
try {
    return (String) xpath.evaluate(nodePath, inputSource, XPathConstants.STRING);
} catch (XPathExpressionException e) {
    return "";
NodeListextractNodeList(XPath xpath, String nodePath, String xmlString)
extract Node List
InputSource inputSource = new InputSource(new StringReader(xmlString));
try {
    return (NodeList) xpath.evaluate(nodePath, inputSource, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
    return null;
NodeListextractNodes(File xmlFile, String xpathLocator)
extract Nodes
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(xpathLocator);
return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
StringextractValue(String xml, String xpathExpression)
extract Value
String actual;
try {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
    byte[] bytes = xml.getBytes("UTF-8");
    InputStream inputStream = new ByteArrayInputStream(bytes);
...
ListfindElements(final String xPathExpression, final Element root)
Checks in under a given root element whether it can find a child elements which match the XPath expression supplied.
final List<Element> elements = new ArrayList<Element>();
NodeList nodes = null;
try {
    XPathExpression expr = COMPILED_EXPRESSION_CACHE.get(xPathExpression);
    if (expr == null) {
        expr = XPATH.compile(xPathExpression);
        COMPILED_EXPRESSION_CACHE.put(xPathExpression, expr);
    nodes = (NodeList) expr.evaluate(root, XPathConstants.NODESET);
} catch (final XPathExpressionException e) {
    throw new IllegalArgumentException("Unable evaluate xpath expression", e);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
    elements.add((Element) nodes.item(i));
return elements;
booleanhasAnimatedPng(ImageReader reader)
has Animated Png
if (!"png".equals(reader.getFormatName())) {
    return false;
try {
    IIOMetadata metadata = reader.getImageMetadata(0);
    XPath xPath = XPathFactory.newInstance().newXPath();
    for (String name : metadata.getMetadataFormatNames()) {
        Node root = metadata.getAsTree(name);
...
voidisLegalXPath(final String xPath)
is Legal X Path
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.compile(xPath);
booleanisValidFilter(String xPathString)
is Valid Filter
if (xPathString == null) {
    return true;
try {
    XPath xPath = xPathFactory.newXPath();
    xPath.compile(xPathString);
    return true;
} catch (XPathExpressionException ex) {
...
booleanisXPathAbsolute(String path)
is X Path Absolute
validateXPath(path);
return path.matches(REGEX_PATH_ABS);