What are the potential pitfalls when using DOMDocument() and DOMXpath() in PHP?

When using DOMDocument() and DOMXpath() in PHP, a potential pitfall is not handling errors properly, such as when loading invalid XML or querying for elements that may not exist. To solve this, it's important to check for errors and handle them gracefully to prevent the script from breaking.

$doc = new DOMDocument();
$doc->loadXML($xml);

if (!$doc) {
    die('Error loading XML');
}

$xpath = new DOMXpath($doc);

$elements = $xpath->query('//someElement');

if ($elements === false) {
    die('Error querying for elements');
}

foreach ($elements as $element) {
    // Do something with the element
}