I’m trying to traverse a DOM tree in PHP using DOMDocument. Initial calls to getElementById / getElementsByTagName are successful, but I’m not sure how to proceed with the resulting NodeList.
Here’s an example HTML file that I’m trying to traverse.
<!DOCTYPE html>
<html>
<div id="container">
<p> Hello </p>
</div>
</html>
In Javascript I’d be able to chain DOM traversal methods like so:
document.getElementById('container').getElementsByTagName('p')[0].innerText
// returns "Hello"
However in PHP trying similar …
<?php
$document = new DOMDocument();
$document->load('test.html');
echo $document->getElementById('content')->getElementsByTagName('p')->item(0)->nodeValue . PHP_EOL;
?>
… simply returns this error:
Fatal error: Call to a member function getElementsByTagName() on a non-object in /Users/liam/foobar on line 6
Am I doing something wrong or is this simply not supported?
You don’t have an element with the id
content— it’s calledcontainer.Also, you can’t call
getElementByIdon any old XML document. it needs to have “a DTD which defines an attribute to be of type ID” (from the manual). Telling DOMDocument that the document is HTML (as is done implicitly in the case of Javascript in a browser) is enough to be able to use the function.Here, you should call
DOMDocument::loadHTMLFileinstead ofload.