Java XPath Get getNodeList(final String expr, final File xmlFile)

Here you can find the source of getNodeList(final String expr, final File xmlFile)

Description

Evaluates given XPath expression and returns node list.

License

Apache License

Parameter

Parameter Description
expr XPath expression
fileName path of the XML file to be parsed

Exception

Parameter Description
XPathExpressionException an exception
ParserConfigurationException an exception
SAXException an exception
IOException an exception

Declaration

public static NodeList getNodeList(final String expr, final File xmlFile)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException 

Method Source Code


//package com.java2s;
/*// w  ww .  ja  va  2 s .co m
 Copyright 2013 Microsoft Open Technologies, Inc.
     
 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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Evaluates given XPath expression and returns node list. 
     * @param expr XPath expression
     * @param fileName path of the XML file to be parsed
     * @return 
     * @throws XPathExpressionException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static NodeList getNodeList(final String expr, final File xmlFile)
            throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {

        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate(expr, parseXMLFile(xmlFile), XPathConstants.NODESET);

        return nodeList;
    }

    /**
     * Parses contents of given XML file and returns DOM Object
     * @param fileName Path of XML file 
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document parseXMLFile(final File xmlFile)
            throws ParserConfigurationException, SAXException, IOException {

        if (xmlFile == null)
            return null;

        Document doc = null;
        DocumentBuilder docBuilder;
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        docBuilder = docBuilderFactory.newDocumentBuilder();

        doc = docBuilder.parse(xmlFile);
        return doc;
    }
}

Related

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