WSK
WSK

Reputation: 6138

Stuck with XPath

Can somebody find what is wrong with this code. It always returns o nodes no matter whatever XPath I chose

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
domFactory.setIgnoringComments(true);
domFactory.setIgnoringElementContentWhitespace(true);

DocumentBuilder builder = domFactory.newDocumentBuilder();
Document dDoc = builder.parse("P:/MyBooks.xml");
NodeList myNodes = (NodeList) xpath.evaluate("//Title", dDoc, XPathConstants.NODESET);
System.out.println(myNodes.getLength());

MyBookx.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<Books>
   <Title attrib="title1"/>
   <Title attrib="title2"/>
   <Title attrib="title3"/>
</Books>

Upvotes: 2

Views: 177

Answers (1)

WSK
WSK

Reputation: 6138

I was doing a big mistake. My xml doc was using default namespace while I am setting NamespaceAware(true) in docFactory. So I set NamespaceAware(false) and my problem is solved

Upvotes: 2

Related Questions