How can PHP developers efficiently access attributes in XML files using DOMDocument?

When working with XML files in PHP using DOMDocument, developers can efficiently access attributes by using the getAttribute() method. This method allows developers to retrieve the value of a specific attribute for a given element in the XML file. By specifying the attribute name as a parameter to getAttribute(), developers can easily retrieve attribute values without having to iterate through all attributes of an element.

// Load the XML file
$xml = new DOMDocument();
$xml->load('example.xml');

// Get the root element
$root = $xml->documentElement;

// Get the value of the 'id' attribute for the 'example' element
$exampleElement = $root->getElementsByTagName('example')->item(0);
$idValue = $exampleElement->getAttribute('id');

echo $idValue;