What are some common pitfalls to watch out for when using DOMDocument in PHP for XML manipulation?

One common pitfall when using DOMDocument in PHP for XML manipulation is not handling errors properly. It's important to check for errors during loading and parsing XML documents to avoid unexpected behavior. Use the libxml_use_internal_errors() function to enable error handling and the libxml_get_errors() function to retrieve any errors.

// Enable error handling
libxml_use_internal_errors(true);

// Load and parse XML document
$doc = new DOMDocument();
$doc->load('example.xml');

// Check for errors
$errors = libxml_get_errors();
if (!empty($errors)) {
    foreach ($errors as $error) {
        echo "Error: " . $error->message . "\n";
    }
}

// Clear errors
libxml_clear_errors();