Un contexte d'espace de noms peut être établi sur l'objet XPath par l'intermédiaire de la méthode setNamespaceContext() prenant comme argument un objet NamespaceContext.
Cet objet doit implémenter l'interface NamespaceContext et être initialisé avec l'adresse URI d'espace de noms et le préfixe adéquats.
Dans ce contexte, il est nécessaire d'activer la prose en compte des espaces de noms en utilisant la méthode setNamespaceAware() de la fabrique XPathFactory.
import java.util.ArrayList; import java.util.Collection; import java.util.Hashtable; import java.util.Iterator; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; public class EspaceNomsContextuel implements NamespaceContext { private Hashtable prefixeVersURI; private Hashtable URIVersPrefixe; public EspaceNomsContextuel() { this("http://www.laltruiste.com/", "lal"); } public EspaceNomsContextuel(String uri, String prefixe) { this.prefixeVersURI = new Hashtable(); this.URIVersPrefixe = new Hashtable(); this.prefixeVersURI.put(prefixe, uri); this.URIVersPrefixe.put(uri, prefixe); } public String getNamespaceURI(String prefix) { Object uri = this.prefixeVersURI.get(prefix); return uri != null ? uri.toString() : XMLConstants.NULL_NS_URI; } public String getPrefix(String namespaceURI) { Object prefixe = this.URIVersPrefixe.get(namespaceURI); return prefixe != null ? prefixe.toString() : XMLConstants.NULL_NS_URI; } public Iterator getPrefixes(String namespaceURI) { Object prefixe = this.URIVersPrefixe.get(namespaceURI); ArrayList liste = new ArrayList(); if(prefixe != null) liste.add(prefixe); return liste.iterator(); } } 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.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class ExpressionXPath { public static void main(String[] args) { try { DocumentBuilderFactory fabriqueDOM = DocumentBuilderFactory.newInstance(); fabriqueDOM.setNamespaceAware(true); fabriqueDOM.setValidating(false); DocumentBuilder analyseur = fabriqueDOM.newDocumentBuilder(); Document document = analyseur.parse(new File("logitheque.xml")); XPathFactory fabriqueXPath = XPathFactory.newInstance(); XPath xpath = fabriqueXPath.newXPath(); NamespaceContext contexte = new EspaceNomsContextuel(); xpath.setNamespaceAware(contexte); String expression = "//logiciel[@code='15202718']"; Node noeud = (Node) xpath.evaluate( expression, document, XPathConstants.NODE); afficherInfos(noeud); expression = "ancestor::*/logiciel[3]/commentaire"; noeud = (Node) xpath.evaluate( expression, noeud, XPathConstants.NODE); afficherInfos(noeud); } catch(IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } } public static void afficherInfos(Node noeud) { String nom = noeud.getNodeName(); String valeur = noeud.getNodeValue(); short type = noeud.getNodeType(); if (type == Node.ELEMENT_NODE) valeur = noeud.getTextContent(); System.out.println(nom + " (" + type + ") = '" + valeur + "'"); } }