Java XPath Select selectStrings(String xpath, Object node)

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

Description

select Strings

License

Open Source License

Declaration

public static List<String> selectStrings(String xpath, Object node) 

Method Source Code


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

import java.util.ArrayList;

import java.util.List;
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 List<String> selectStrings(String xpath, Object node) {
        List<String> values = new ArrayList<String>();
        for (Node it : selectNodes(xpath, node)) {
            String textContent = getTextContent(it);
            if (textContent.length() > 0) {
                values.add(textContent);
            }//from   w w  w .j  a  v a  2 s. co  m
        }
        return values;
    }

    public static Node[] selectNodes(String xpath, Object node) {
        return streamNodes(xpath, node).toArray(Node[]::new);
    }

    /**
     * Get text content of the first child node matching the given node name. Use this method instead of {@link #selectString(String, Object)} whenever xpath support is not required, because it is much faster, especially for large documents.
     *
     * @param childName
     *            search for nodes with this name
     * @param parentNode
     *            search in the child nodes of this nodes
     * @return text content of the child node or null if no child with the given name was found
     */
    public static String getTextContent(String childName, Node parentNode) {
        Node child = getChild(childName, parentNode);

        if (child == null) {
            return null;
        }

        return getTextContent(child);
    }

    public static String getTextContent(Node node) {
        StringBuilder sb = new StringBuilder();

        for (Node textNode : getChildren("#text", node)) {
            sb.append(textNode.getNodeValue());
        }

        return sb.toString().trim();
    }

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

    /**
     * @param nodeName
     *            search for nodes with this name
     * @param parentNode
     *            search in the child nodes of this nodes
     * @return text content of the child node or null if no child with the given name was found
     */
    public static Node getChild(String nodeName, Node parentNode) {
        if (parentNode == null) {
            return null;
        } else {
            return stream(parentNode.getChildNodes()).filter(n -> nodeName.equals(n.getNodeName())).findFirst()
                    .orElse(null);
        }
    }

    public static Node[] getChildren(String nodeName, Node parentNode) {
        if (parentNode == null) {
            return new Node[0];
        } else {
            return stream(parentNode.getChildNodes()).filter(n -> nodeName.equals(n.getNodeName()))
                    .toArray(Node[]::new);
        }
    }

    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. selectSingleElement(Element element, String xpathExpression)
  2. selectSingleNode(final Node node, final String xPath)
  3. selectSingleNode(final Node sourceNode, final String xPathExpression)
  4. selectSingleNode(Node node, String xpath, NamespaceContext context)
  5. selectString(String xpath, Object node)
  6. selectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)
  7. xmlSelectNodes(Node node, String xpathExpression)
  8. XPathAPI_selectNodeList(Document doc, String xpath, Node namespaceNode)
  9. xpathNode(String expr, Document doc)