I want to query an XML via XPath. This XML has a namespace declared. I 've read that a NameContext on the XPath object has to be set and namespace awareness in the document factory should be enabled.
But the output in the getNamespaceURI(#String) method is never print. What I'm doing wrong?
String xpathStr = ... //some string
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(settlementFile);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath(),
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
System.out.println("I'm here");
... //do some work
}
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
});
XPathExpression xpathExpression = xpath.compile(xpathStr);
String result = xpathExpression.evaluate(document);