Search code examples
phphtmlxpathdomdocument

HTML Parsing URL with DOMDocument and DOMXPath - Get element by ID


I've started to develop an script so I can parse an HTML DOM elements.

Here is what I have done already:

<?PHP
// to retrieve selected html data, try these DomXPath examples:

$url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822';
libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$elements = $xpath->query("*/span[@id='ProductName']");

if (!is_null($elements)) {
  foreach ($elements as $element) {
    echo "<br/>[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}
?>

     

All what I want is to get the text contained in the HTML element <span id="ProductName"></span>

The problem with my script is that I get Blank screen only, no results at all.

Can you please help me out understand how this thing works and make it.

Thanks in advance!


Solution

  • AYou should check whether your query yielded any elements (DOMNodelist). Check it first then get the element.

    $elements = $xpath->query('//span[@id="ProductName"]');
    if($elements->length > 0) {
        echo $elements->item(0)->nodeValue;
    }
    

    Sidenote: cant test this though im on mobile but this should be the basic idea