Java XPath Select selectNodes(final Node node, final String xPath)

Here you can find the source of selectNodes(final Node node, final String xPath)

Description

select Nodes

License

Apache License

Declaration

public static List<Node> selectNodes(final Node node, final String xPath) throws XPathExpressionException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.AbstractList;

import java.util.List;

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 {
    private static XPathFactory _xpf = XPathFactory.newInstance();

    public static List<Node> selectNodes(final Node node, final String xPath) throws XPathExpressionException {
        return wrapNodeList((NodeList) _xpf.newXPath().evaluate(xPath, node, XPathConstants.NODE));
    }/*from  ww w .j a  va2 s  .  co m*/

    public static List<Node> wrapNodeList(final NodeList nodeList) {
        return new AbstractList<Node>() {
            @Override
            public Node get(final int index) {
                return nodeList.item(index);
            }

            @Override
            public int size() {
                return nodeList.getLength();
            }
        };
    }
}

Related

  1. getXmlElements(Document inXml, String xpath)
  2. selectElements(Element element, String xpathExpression)
  3. selectNodeIterator(Node nContextNode, String sXPath)
  4. selectNodeList(Node contextNode, String expression)
  5. selectNodeList(Node node, String expression)
  6. selectNodes(Node nodeParent, String name)
  7. selectNodes(String express, Object source)
  8. selectNodes(String xPath, Node target)
  9. selectNodes(String xpath, Object node)