I believe that it worked some time ago, but now xpath returns null. Can someone help me find my stupid mistake in the following code? Or will I have to provide a 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(); }
The XML document is here:
<?xml version="1.0" encoding="UTF-8"?> <root xmlns="http://www.example.com/schema"> <class /> <class /> </root>
source share