tags:

views:

51

answers:

1
+2  Q: 

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>
+2  A: 

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

Leslie Norman