PHP DOMXPath always returning empty object


I’m new to DOMXPath, and am basing off of examples I’ve found on the web, but no matter what page I try to parse (my own sites, google groups), I’m getting an empty DOMXPath object.

// parse page
$dom = new DOMDocument();
if (! @$dom->loadHTML($html))
return FALSE;
echo $dom->saveHTML();

$xpath = new DOMXPath($dom);

$data[‘jdata’] = var_dump($xpath);
$elements = $xpath->query("/html");
$data[‘jdata’] = var_dump($elements);

The echo $dom->saveHTML() call shows the html, but the var_dumps produce:

object(DOMXPath)#27 (0) {
object(DOMNodeList)#29 (0) {

Any ideas?



Everything you’re doing is fine, with one silly exception — var_dump doesn’t display any useful output on DOM objects. You’ll need to pull out the nodes from the DOMNodeList and look at them yourself.


Thanks, Andrew!

I sort of stumbled upon that tidbit myself, subsequent to posting. (I also should have paid more attention to where/how DOMXPath fit into the DOM/XML hierarchy when I first looked at the manual page.)

btw - do you know of a good reference and/or tutorial for XPATH queries?