PHP DOMDocument calling getElementsByTagName on a NodeList

余生长醉 提交于 2020-01-15 18:50:50

问题


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?


回答1:


You don't have an element with the id content -- it's called container.

Also, you can't call getElementById on 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::loadHTMLFile instead of load.




回答2:


It would seem to me that $document->getElementById('content') is empty, you need to change it to $document->getElementById('container').




回答3:


Try xpath: http://php.net/manual/en/class.domxpath.php

<?php
$xpath = new DOMXPath($document);

$node = $xpath->query('//*[@id="container"]//p')->item(0);
if ($node instanceof DOMNode) {
  echo $node->nodeValue . PHP_EOL;
}


来源:https://stackoverflow.com/questions/6046519/php-domdocument-calling-getelementsbytagname-on-a-nodelist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!