Java tutorial
//package com.java2s; /* * DSS - Digital Signature Services * * Copyright (C) 2011 European Commission, Directorate-General Internal Market and Services (DG MARKT), B-1049 Bruxelles/Brussel * * Developed by: 2011 ARHS Developments S.A. (rue Nicolas Bov 2B, L-1253 Luxembourg) http://www.arhs-developments.com * * This file is part of the "DSS - Digital Signature Services" project. * * "DSS - Digital Signature Services" is free software: you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. * * DSS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with * "DSS - Digital Signature Services". If not, see <http://www.gnu.org/licenses/>. */ import java.util.Iterator; import javax.xml.crypto.dsig.XMLSignature; import javax.xml.namespace.NamespaceContext; 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.Node; import org.w3c.dom.NodeList; public class Main { /** * Return the Element corresponding the the XPath * * @param xmlNode * @param xpathString * @return * @throws XPathExpressionException */ public static NodeList getNodeList(Node xmlNode, String xpathString) { try { XPathExpression expr = createXPathExpression(xpathString); return (NodeList) expr.evaluate(xmlNode, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new RuntimeException(e); } } private static XPathExpression createXPathExpression(String xpathString) { /* XPath */ XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); xpath.setNamespaceContext(new NamespaceContext() { @Override public Iterator<?> getPrefixes(String namespaceURI) { throw new RuntimeException(); } @Override public String getPrefix(String namespaceURI) { throw new RuntimeException(); } @Override public String getNamespaceURI(String prefix) { if ("ds".equals(prefix)) { return XMLSignature.XMLNS; } else if ("xades".equals(prefix)) { return "http://uri.etsi.org/01903/v1.3.2#"; } else if ("xades141".equals(prefix)) { return "http://uri.etsi.org/01903/v1.4.1#"; } else if ("xades111".equals(prefix)) { return "http://uri.etsi.org/01903/v1.1.1#"; } throw new RuntimeException("Prefix not recognized : " + prefix); } }); try { XPathExpression expr = xpath.compile(xpathString); return expr; } catch (XPathExpressionException ex) { throw new RuntimeException(ex); } } }