With DOMElement objects, the element name is not accessible using a getName()
function, but as property $tagName
:
echo $node->tagName;
getName()
is only available with SimpleXMLElement, which is another XML/XPath API for PHP.
Вопрос
I am in the middle of a process that should extract something from a HTML page. I am fairly new to DomDocument in PHP, but I got this together from some tutorials and Stack Overflow.
Unfortunately, I need to know what element I am currently getting in the foreach loop below. As far as I know, the getName() function has something to do with XML, because it gives an Undefined Function Fatal error. Do you guys know any way to do this?
$rawdom = new DOMDocument();
$rawdom->loadHTML($page);
$finder = new DomXPath($rawdom);
$nodes = $finder->query("//dl[contains(@class, 'layout__definitionlist')]");
$tmp_dom = new DOMDocument();
foreach ($nodes as $node) {
echo $node->getName();
$tmp_dom->appendChild($tmp_dom->importNode($node,true));
}
$innerHTML = $tmp_dom->saveHTML();
echo $innerHTML;
Решение
With DOMElement objects, the element name is not accessible using a getName()
function, but as property $tagName
:
echo $node->tagName;
getName()
is only available with SimpleXMLElement, which is another XML/XPath API for PHP.