Search code examples
phpdomxpath

DOMXPath not working


I am trying to get the price of products in a webshop but DOMXPath doest seem to be working.

The server is running php 5.5 and LibXML is enabled. No errors are returned, only a length of zero.

 ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();

$xmlsource = 'https://tennistoko.nl/product/professional-supreme-comfort-grip-3-st';

$d = new DOMDocument();
$d->loadHTML($xmlsource);
$xpath = new DOMXPath($d);




$nodes = $xpath->query('//*[@itemprop]');  //this catches all elements with itemprop attribute
foreach ($nodes as $node) { 
   // do your stuff here with $node
   print_r($node);
}    


print_r($nodes);

Solution

  • loadHTML is for loading HTML from a string, to load from file or url use loadHTMLFile.

    $xmlsource = 'https://tennistoko.nl/product/professional-supreme-comfort-grip-3-st';
    
    $d = new DOMDocument();
    @$d->loadHTMLFile($xmlsource);      // @ if for suppressing warnings
    $xpath = new DOMXPath($d);
    
    $nodes = $xpath->query('//*[@itemprop]');  //this catches all elements with itemprop attribute
    foreach ($nodes as $node) { 
       // do your stuff here with $node
       print_r($node);
    }