Java tutorial
//package com.java2s; import java.util.ArrayList; import javax.xml.namespace.QName; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static String XpathFactory = "net.sf.saxon.xpath.XPathFactoryImpl"; public static ArrayList<String> getNodeListAttValAsStringCol(String Xpath, Node node, String attrName) throws Exception { ArrayList<String> retV = new ArrayList<String>(); NodeList nl = getNodesListXpathNode(Xpath, node); int l = nl.getLength(); Element e = null; String val = ""; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { val = e.getAttribute(attrName); if (val != null && val.length() > 0) { //log.info("getNodeListAttValAsStringCol val = "+val +" attname = "+attrName); /*try { log.info(convertToStringLeaveCDATA(e)); }catch(Exception E) { E.printStackTrace(); }*/ retV.add(val); } } } return retV; } public static NodeList getNodesListXpathNode(String Xpath, Node node) throws Exception { return (NodeList) getNodesListXpath(Xpath, node, "", "", XPathConstants.NODESET); } public static Object getNodesListXpath(String XpathS, Node node, String nsuri, String pre, QName returnType) throws Exception { Object matches = null; // TODO move this to a generic start up method System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XpathFactory); XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); XPath xpath = xpathFactory.newXPath(); XPathExpression xpe = xpath.compile(XpathS); matches = xpe.evaluate(node, returnType); return matches; } }