Java tutorial
//package com.java2s; /** * 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; } }