Java tutorial
//package com.java2s; import javax.xml.xpath.*; import org.w3c.dom.Node; public class Main { private static XPathFactory xpathFactory = null; private static XPath xpath = null; /** * This method yields the Attribute at the node extracted by the expression. * @param parentNode The node to start at. * @param expression The XPath expression to evaluate. * @param attribute The Name of the attribute you would like to extract from the * XPath'd node. * @return The Value of the attribute, or the empty String if no matching node * or matching attribute could be found. * @throws XPathExpressionException * * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a> */ public static String getAttribute(Node parentNode, String expression, String attribute) throws XPathExpressionException { String attr = ""; Node n = getNode(parentNode, expression); if (n != null) { n = n.getAttributes().getNamedItem(attribute); } if (n != null) { attr = n.getNodeValue(); } return attr; } /** * This method uses XPath to get you a child node of the provided node. * @param parentNode The node to start at. * @param expression The XPath Expression to evaluate. * @return The Node that was retrieved by the provided node and xpath expression. * @throws XPathExpressionException * * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a> */ public static Node getNode(Node parentNode, String expression) throws XPathExpressionException { return (Node) getXPath().evaluate(expression, parentNode, XPathConstants.NODE); } /** * This method gives you an XPath object from the XPath Factory. * @return A new XPath object. * * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a> */ protected static synchronized XPath getXPath() { if (xpath == null) { xpath = getXPathFactory().newXPath(); } return xpath; } /** * This method yields the XPath Factory (creating it if necessary). * @return The XPathFactory object. * * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a> */ protected static synchronized XPathFactory getXPathFactory() { if (xpathFactory == null) { xpathFactory = XPathFactory.newInstance(); } return xpathFactory; } }