What are some common methods for reading XML files in PHP?

When working with XML files in PHP, there are several common methods for reading the data contained within them. One of the most popular methods is to use the SimpleXMLElement class, which allows for easy traversal and extraction of data from XML files. Another common method is to use the DOMDocument class, which provides a more powerful and flexible way to work with XML documents. Additionally, PHP also provides functions like simplexml_load_file() and simplexml_load_string() to parse XML data.

// Method 1: Using SimpleXMLElement
$xml = simplexml_load_file('example.xml');
foreach($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br>";
}

// Method 2: Using DOMDocument
$doc = new DOMDocument();
$doc->load('example.xml');
$elements = $doc->getElementsByTagName('element');
foreach($elements as $element) {
    echo $element->nodeValue . "<br>";
}