I believe it was working sometime ago but now xpath is returning null. Can somebody help me find my stupid mistake in following code?
Or I will have to provide NamespaceContext even after setNamespaceAware(false)?
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(false);
domFactory.setIgnoringComments(true);
domFactory.setIgnoringElementContentWhitespace(true);
try {
Document doc = domFactory.newDocumentBuilder().parse(new File("E:/Temp/test.xml"));
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.evaluate("//class", doc, XPathConstants.NODESET);
System.out.println(nl.getLength());
}catch (Exception e){
e.printStackTrace();
}
XML document is here:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.example.com/schema">
<class />
<class />
</root>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…