What are the best practices for handling attributes and nested elements in XML files when using SimpleXML in PHP?

When working with XML files using SimpleXML in PHP, it's important to handle attributes and nested elements properly. To access attributes, you can use the `attributes()` method, and to access nested elements, you can simply use object notation. It's also a good practice to check if attributes or elements exist before trying to access them to avoid errors.

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

// Accessing attributes
$attributeValue = $xml->element->attributes()->attributeName;

// Accessing nested elements
$nestedElementValue = $xml->parentElement->childElement;

// Checking if attribute or element exists before accessing it
if(isset($xml->element->attributes()->attributeName)) {
    // Do something with the attribute
}

if(isset($xml->parentElement->childElement)) {
    // Do something with the nested element
}