What are some best practices for handling errors and exceptions when working with external XML files in PHP?

When working with external XML files in PHP, it is important to handle errors and exceptions gracefully to prevent unexpected behavior or crashes in your application. One best practice is to use try-catch blocks to catch and handle any exceptions that may occur while parsing or manipulating the XML data.

try {
    $xml = simplexml_load_file('external_file.xml');
    
    // Access and manipulate XML data here
    
} catch (Exception $e) {
    // Handle any exceptions that occur
    echo 'Error loading XML file: ' . $e->getMessage();
}