What is the purpose of using DOMDocument and DOMXPath in the provided PHP code?

The purpose of using DOMDocument and DOMXPath in the provided PHP code is to parse and navigate XML or HTML documents. DOMDocument is used to load the XML/HTML content and create a DOM object representing the document structure, while DOMXPath is used to query specific elements within the document using XPath expressions.

// Load the XML content into a DOMDocument
$doc = new DOMDocument();
$doc->loadXML($xmlContent);

// Create a DOMXPath object to query elements within the document
$xpath = new DOMXPath($doc);

// Example: Querying all <title> elements within the XML document
$titles = $xpath->query('//title');
foreach ($titles as $title) {
    echo $title->nodeValue . "\n";
}