Document Object Model (DOM)¶
The Document Object Model, also called DOM, is a programming interface for HTML and XML documents. It represents the document as a tree of nodes that can be traversed, queried, and modified.
PHP provides the dom extension, which implements the W3C DOM specification via classes such as DOMDocument, DOMElement, DOMNode, DOMXPath, and DOMNodeList.
PHP 8.4 introduced Dom\HTMLDocument and Dom\XMLDocument as improved, spec-compliant replacements for the legacy DOMDocument, with better error handling and HTML5 parsing.
<?php
$doc = new DOMDocument();
$doc->loadHTML('<p class="greeting">Hello, <strong>world</strong></p>');
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//p[@class="greeting"]/strong');
foreach ($nodes as $node) {
echo $node->textContent; // world
}
?>
See also PHP DOM extension.
Related : Extensible Markup Language (XML), HyperText Markup Language (HTML), DOM, SimpleXML, XMLReader, XMLwriter, Javascript, DOMChildNode, DOMParentNode, Scalable Vector Graphics (SVG)