Java tutorial
//package com.java2s; /** Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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 static javax.xml.xpath.XPathConstants.NODE; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.StringReader; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.xml.sax.InputSource; public class Main { /** * Returns the string value of the named attribute in an XML file at the coordinates specified by the passed XPath expression * @param fileName The file name * @param xPathExpression The XPath expression * @param attributeName The attribute name * @return the attribute valye */ public static String getAttribute(String fileName, String xPathExpression, String attributeName) { try { Document document = parseXML(new File(fileName)); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression xpathExpression = xpath.compile(xPathExpression); Node node = (Node) xpathExpression.evaluate(document, NODE); return getAttributeValueByName(node, attributeName); } catch (Exception e) { throw new RuntimeException("Failed to extract element from:" + fileName, e); } } /** * Parses an input source and generates an XML document. * @param is An input source to an XML source. * @return An XML doucument. */ public static Document parseXML(InputSource is) { try { Document doc = null; DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = documentBuilder.parse(is); return doc; } catch (Exception e) { throw new RuntimeException("Failed to parse XML source", e); } } /** * Parses an input stream and generates an XML document. * @param is An input stream to an XML source. * @return An XML doucument. */ public static Document parseXML(InputStream is) { return parseXML(new InputSource(is)); } /** * Parses a file and generates an XML document. * @param file The file to parse * @return An XML doucument. */ public static Document parseXML(File file) { FileInputStream fis = null; try { fis = new FileInputStream(file); return parseXML(fis); } catch (Exception e) { throw new RuntimeException("Failed to open XML file:" + file, e); } finally { try { fis.close(); } catch (Exception e) { } } } /** * Parses the input stream of a URL and generates an XML document. * @param xmlUrl The URL of the XML document. * @return The parsed document. */ public static Document parseXML(URL xmlUrl) { InputStream is = null; BufferedInputStream bis = null; try { is = xmlUrl.openConnection().getInputStream(); bis = new BufferedInputStream(is); return parseXML(bis); } catch (Exception e) { throw new RuntimeException("Failed to read XML URL:" + xmlUrl, e); } finally { try { bis.close(); } catch (Exception e) { } try { is.close(); } catch (Exception e) { } } } /** * Parses an XML string and generates an XML document. * @param xml The XML to parse * @return An XML doucument. */ public static Document parseXML(CharSequence xml) { StringReader sr = new StringReader(xml.toString()); return parseXML(new InputSource(sr)); } /** * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found. * If it is not found, returns a null. * @param nnm NamedNodeMap * @param name String * @return String */ public static String getAttributeValueByName(NamedNodeMap nnm, String name) { if (nnm == null) return null; for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { return attr.getValue(); } } return null; } /** * Returns the attribute value for the passed name in the passed node. * @param node the node to get the attribute from * @param name the name of the attribute * @return The attribute value or null if it is not found. */ public static String getAttributeValueByName(Node node, String name) { return getAttributeValueByName(node.getAttributes(), name); } }