I’m trying to extract a org.w3c.dom.NodeList from http://www.amazon.com/gp/bestsellers/electronics in Java. Here’s the XPath statement I used to display the results I’m after in the Google Chrome JavaScript console:
$x("//div[@class='zg_itemRightDiv_normal']")
The above statement worked flawlessly, but when I use Java’s javax.xml.xpath and org.w3c.dom libraries, I get no results:
XPathExpression expr = xPath.compile("//div[@class='zg_itemRightDiv_normal']");
NodeList productNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
// In the above productNodes definition, doc is a org.w3c.dom.DocumentFragment
System.out.println(productNodes.getLength()); // prints out '0'
What am I missing?
The solution to this problem ended up being twofold:
DocumentFragmentI was reading from didn’t include the information I was attempting to extract; using the fullDocumentsolved the issuexPath.compile("//DIV[@class='zg_itemRightDiv_normal']")).