What are common pitfalls when using SimpleXML in PHP for parsing XML files?

One common pitfall when using SimpleXML in PHP for parsing XML files is not handling errors properly, which can lead to unexpected behavior or fatal errors. To solve this, it's important to check for errors during parsing and handle them gracefully.

// Load XML file
$xml = simplexml_load_file('example.xml');

// Check for errors during parsing
if ($xml === false) {
    die('Error loading XML file');
}

// Use SimpleXML object for further processing
foreach ($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br>";
}