Java XPath Expression streamNodes(String xpath, Object node)

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

Description

stream Nodes

License

Open Source License

Declaration

public static Stream<Node> streamNodes(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 Stream<Node> streamNodes(String xpath, Object node) {
        return stream((NodeList) evaluateXPath(xpath, node, XPathConstants.NODESET));
    }//from ww  w.j a  va  2  s  . c o  m

    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. parseXPath(String expression, NamespaceContext nsContext)
  2. pathExists(XPath xpath, String expression, Object object)
  3. readXmlNodeChildFromFile(String xPath, String path, NamespaceContext nsContext)
  4. replaceXPathNode(Node replaceThis, Node replaceWith)
  5. singleXPathNode(String expression, Node node)
  6. string(Node context, String expression)
  7. string(String fileName, String xpathExpression)
  8. valueOf(Node node, String xpath, NamespaceContext context)
  9. xmlValueOf(Node node, String xpathExpression)