get Node List from Document - Java XML

Java examples for XML:DOM Node Value

Description

get Node List from Document

Demo Code


//package com.java2s;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static NodeList getNodeList(Document doc, String xPathAsString)
            throws XPathExpressionException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(xPathAsString);
        return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    }/*w ww . j a  v  a2 s .  c  o m*/

    public static NodeList getNodeList(Node node, String xPathAsString)
            throws XPathExpressionException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(xPathAsString);
        return (NodeList) expr.evaluate(node, XPathConstants.NODESET);
    }
}

Related Tutorials