Java XPath Get getNodesByXPath(Element parent, String name)

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

Description

get Nodes By X Path

License

Open Source License

Declaration

public static List<Node> getNodesByXPath(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<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));
        }//  w  w  w .ja v  a 2 s . c  om

        return result;
    }

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

Related

  1. getNodeListAsArray(Node doc, String xpath)
  2. getNodeListAttValAsStringCol(final String xPath, final Node node, final String attrName)
  3. getNodes(Node node, String expStr)
  4. getNodesByPath(String path, Element localElement, Document doc)
  5. getNodesByXPath(Document doc, XPathExpression expr)
  6. getNodesListXpathNode(final String xPath, final Node node)
  7. getNodeText(XPath xpath, String xp, Node n)
  8. getNodeTextByXPath(Document doc, String xpath)
  9. getNogeList(String path, Node node)