Java XPath Get getElementsByXPath(Element parent, String name)

Here you can find the source of getElementsByXPath(Element parent, String name)

Description

get Elements By X Path

License

Open Source License

Declaration

public static List<Element> getElementsByXPath(Element parent, String name) throws XPathExpressionException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static List<Element> getElementsByXPath(Element parent, String name) throws XPathExpressionException {
        List<Element> result = new ArrayList<>();
        for (Node node : getNodesByXPath(parent, name)) {
            if (node instanceof Element) {
                result.add((Element) node);
            }//from w w  w  . j a  va  2  s  .  c  o m
        }

        return result;
    }

    public static List<Node> getNodesByXPath(Element parent, String name) throws XPathExpressionException {
        List<Node> result = new ArrayList<>();

        NodeList nodeList = findNodesByXPath(parent, name);
        for (int i = 0, l = nodeList.getLength(); i < l; i++) {
            result.add(nodeList.item(i));
        }

        return result;
    }

    public static NodeList findNodesByXPath(Node contextNode, String expression) throws XPathExpressionException {
        return (NodeList) XPathFactory.newInstance().newXPath().evaluate(expression, contextNode,
                XPathConstants.NODESET);
    }
}

Related

  1. getCruxPagesXPath()
  2. getDefaultXPath()
  3. getDefaultXPathFactory()
  4. getDigestValue(Node reference)
  5. getElementByXpath(Element sourceRoot, String xpathExpress)
  6. getElementXPath(Node elt)
  7. getExcelPath()
  8. getFactory()
  9. getFastXPath(Node node)