-1

I'm getting a Call to undefined method DOMDocument::xpath() on the $level1 line of my code:

          $doc = new DOMDocument();
          $doc->strictErrorChecking = FALSE;
          $doc->loadHTML($html);
          $xml = simplexml_import_dom($doc);
          $level1 = $doc->xpath('//*[contains(concat(" ", normalize-space(@class), " "), " category-wrap ")]/h2/a');

Here is the html it's loading (via file_get_contents()):

<div class="category-wrap">
   <a href='#'>LINK</a>
   <h2><a href="#">Trying to get this....</a></h2>
   <p>A description</p>

</div>

I'm using this answer as a reference for the xpath query.

What is wrong with my query?

Community
  • 1
  • 1
Jared
  • 1,795
  • 6
  • 32
  • 55
  • Nothing is wrong with your query, there only lacks understanding of the error message. it's easy to fix, just take the right object. – hakre Jul 10 '13 at 09:23

1 Answers1

3

There isn't such method like xpath in DOMDocument. See http://php.net/manual/en/class.domdocument.php

You need to initialize an instance of DOMXpath to use xpath queries

$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);

or alternatively call the method on the SimpleXMLObject you've created

$level1 = $xml->xpath('//*[contains(concat(" ", normalize-space(@class), " "), " category-wrap ")]/h2/a');
WooDzu
  • 4,771
  • 6
  • 31
  • 61