What are the advantages of using DOMDocument or XMLReader over simplexml for parsing XML content in PHP?

When parsing XML content in PHP, using DOMDocument or XMLReader can offer better performance and flexibility compared to simplexml. DOMDocument allows for easy navigation of the XML document tree and manipulation of nodes, while XMLReader is a more memory-efficient option for large XML files as it reads the document sequentially without loading the entire file into memory.

// Using DOMDocument to parse XML content
$xml = '<root><item>Item 1</item><item>Item 2</item></root>';
$dom = new DOMDocument();
$dom->loadXML($xml);

$items = $dom->getElementsByTagName('item');
foreach ($items as $item) {
    echo $item->nodeValue . PHP_EOL;
}

// Using XMLReader to parse XML content
$xml = '<root><item>Item 1</item><item>Item 2</item></root>';
$reader = new XMLReader();
$reader->xml($xml);

while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'item') {
        $reader->read();
        echo $reader->value . PHP_EOL;
    }
}