Java XPath Select selectNodes(String xpath, Object node)

Here you can find the source of selectNodes(String xpath, Object node)

Description

select Nodes

License

Open Source License

Declaration

public static Node[] selectNodes(String xpath, Object node) 

Method Source Code

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

import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Node[] selectNodes(String xpath, Object node) {
        return streamNodes(xpath, node).toArray(Node[]::new);
    }/*from w w w .  j av  a 2  s  .c  o  m*/

    public static Stream<Node> streamNodes(String xpath, Object node) {
        return stream((NodeList) evaluateXPath(xpath, node, XPathConstants.NODESET));
    }

    public static Stream<Node> stream(NodeList nodes) {
        return IntStream.range(0, nodes.getLength()).mapToObj(nodes::item);
    }

    public static Object evaluateXPath(String xpath, Object item, QName returnType) {
        try {
            return XPathFactory.newInstance().newXPath().compile(xpath).evaluate(item, returnType);
        } catch (XPathExpressionException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. selectNodeList(Node node, String expression)
  2. selectNodes(final Node node, final String xPath)
  3. selectNodes(Node nodeParent, String name)
  4. selectNodes(String express, Object source)
  5. selectNodes(String xPath, Node target)
  6. selectNodes(String xpath, Object node)
  7. selectNodesViaXPath(XPath xPath, Node startingNode, String xPathExpression)
  8. selectNodeText(Node node, String expression)
  9. selectSingleElement(Element element, String xpathExpression)