How can the DOMDocument class be used to access elements within a DOM tree in PHP?

To access elements within a DOM tree in PHP, you can use the DOMDocument class. You can load an XML or HTML document into a DOMDocument object, then use methods like getElementById(), getElementsByTagName(), or XPath queries to access specific elements within the document.

// Load the XML file into a DOMDocument object
$doc = new DOMDocument();
$doc->load('example.xml');

// Get elements by tag name
$elements = $doc->getElementsByTagName('elementName');

// Loop through the elements and do something
foreach ($elements as $element) {
    echo $element->nodeValue;
}