import java.io.FileInputStream;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
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.xml.sax.InputSource;
public class Main {
public static void main(String[] args) throws Exception {
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
xp.setNamespaceContext(new MyNamespaceContext());
XPathExpression xpe = xp.compile("ns:feed/ns:entry");
FileInputStream xmlStream = new FileInputStream("input.xml");
InputSource xmlInput = new InputSource(xmlStream);
Element result = (Element) xpe.evaluate(xmlInput, XPathConstants.NODE);
System.out.println(result);
}
}
class MyNamespaceContext implements NamespaceContext {
public String getNamespaceURI(String prefix) {
if ("ns".equals(prefix)) {
return "http://www.w3.org/2005/Atom";
}
return null;
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}