Java XPath Get getNodesByXPath(Document doc, XPathExpression expr)

Here you can find the source of getNodesByXPath(Document doc, XPathExpression expr)

Description

Get xml nodes by xpath expression

License

Open Source License

Parameter

Parameter Description
doc a parameter
expr a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static NodeList getNodesByXPath(Document doc, XPathExpression expr) throws Exception 

Method Source Code


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

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//from  w w  w. j  a v a2  s  .co  m
     * Get xml nodes by xpath expression
     * 
     * @param doc
     * @param expr
     * @return
     * @throws Exception
     */
    public static NodeList getNodesByXPath(Document doc, XPathExpression expr) throws Exception {
        return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    }

    /**
     * Get xml nodes by xpath string, based on xml document
     * 
     * @param doc
     * @param xpath
     * @return
     * @throws Exception
     */
    public static NodeList getNodesByXPath(Document doc, String xpath) throws Exception {
        return (NodeList) getXPathExpression(xpath).evaluate(doc, XPathConstants.NODESET);
    }

    /**
     * Get xml nodes by xpath string, based on xml stream
     * 
     * @param input
     * @param xpath
     * @return
     * @throws Exception
     */
    public static NodeList getNodesByXPath(InputStream input, String xpath) throws Exception {
        return (NodeList) getXPathExpression(xpath).evaluate(getDocument(input), XPathConstants.NODESET);
    }

    /**
     * Get xml nodes by xpath string, based on xml string
     * 
     * @param xml
     * @param xpath
     * @return
     * @throws Exception
     */
    public static NodeList getNodesByXPath(String xml, String xpath) throws Exception {
        return (NodeList) getXPathExpression(xpath).evaluate(getDocument(xml), XPathConstants.NODESET);
    }

    /**
     * Get xml nodes by xpath expression, based on node
     * 
     * @param node
     * @param xpath
     * @return
     * @throws Exception
     */
    public static NodeList getNodesByXPath(Node node, XPathExpression xpath) throws Exception {
        return (NodeList) xpath.evaluate(node, XPathConstants.NODESET);
    }

    /**
     * Get xml nodes by xpath string , based on node
     * 
     * @param node
     * @param xpath
     * @return
     * @throws Exception
     */
    public static NodeList getNodesByXPath(Node node, String xpath) throws Exception {
        return (NodeList) getXPathExpression(xpath).evaluate(node, XPathConstants.NODESET);
    }

    /**
     * Get xpath expression object by xpath string
     * 
     * @param xpathStr
     * @return
     * @throws Exception
     */
    public static XPathExpression getXPathExpression(String xpathStr) throws Exception {
        return XPathFactory.newInstance().newXPath().compile(xpathStr);
    }

    /**
     * Get xml document, by stream
     * 
     * @param input
     * @return
     * @throws Exception
     */
    public static Document getDocument(InputStream input) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(input);
        return doc;
    }

    /**
     * Get xml document, by xml string
     * 
     * @param xml
     * @return
     * @throws Exception
     */
    public static Document getDocument(String xml) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc;
    }
}

Related

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