Java XPath Get getNodeList(File xml, String xpathExpression)

Here you can find the source of getNodeList(File xml, String xpathExpression)

Description

Gets the node list from the given xml file and the given xpath expression.

License

Apache License

Parameter

Parameter Description
xml the xml
xpathExpression the xpath expression

Exception

Parameter Description
XPathExpressionException the x path expression exception
ParserConfigurationException the parser configuration exception
SAXException the sAX exception
IOException Signals that an I/O exception has occurred.

Return

the node list

Declaration

public static NodeList getNodeList(File xml, String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException 

Method Source Code

//package com.java2s;
/**//ww w  .j  av  a2  s.  c om
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.NodeList;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Gets the node list from the given xml file and the given xpath expression.
     * 
     * @param xml
     *            the xml
     * @param xpathExpression
     *            the xpath expression
     * @return the node list
     * @throws XPathExpressionException
     *             the x path expression exception
     * @throws ParserConfigurationException
     *             the parser configuration exception
     * @throws SAXException
     *             the sAX exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static NodeList getNodeList(File xml, String xpathExpression)
            throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xml);
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(xpathExpression);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        return nodes;
    }

    /**
     * Gets the node list from the given xml file and the given xpath expression.
     * 
     * @param xml
     *            the xml file as string.
     * @param xpathExpression
     *            the xpath expression as string.
     * @return the node list
     * @throws XPathExpressionException
     *             the x path expression exception
     * @throws ParserConfigurationException
     *             the parser configuration exception
     * @throws SAXException
     *             the sAX exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static NodeList getNodeList(String xml, String xpathExpression)
            throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xml);
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(xpathExpression);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        return nodes;
    }
}

Related

  1. getNode(Node node, XPathExpression xpath)
  2. getNode(String xPathExpression, Node node)
  3. getNodeByXPath(Document document, String xPath)
  4. getNodeByXPath(Object context, String expression)
  5. getNodeList(Element root, String xpath)
  6. getNodeList(final String expr, final File xmlFile)
  7. getNodeList(InputStream stream, String pattern)
  8. getNodeList(Node node, String path)
  9. getNodeList(Node node, String xpath)