What are some recommended PHP functions for generating an XML tree from arrays and objects?

When working with arrays and objects in PHP, it is common to need to generate XML data from them. One way to accomplish this is by using the SimpleXMLElement class in PHP, which allows you to easily create XML nodes and attributes. Another useful function is xmlrpc_encode, which can encode PHP values into XML format. Additionally, the DOMDocument class provides a powerful way to create XML documents from arrays and objects.

// Example using SimpleXMLElement
$data = ['name' => 'John', 'age' => 30];
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($data, [$xml, 'addChild']);
echo $xml->asXML();

// Example using xmlrpc_encode
$data = ['name' => 'John', 'age' => 30];
$xml = xmlrpc_encode($data);
echo $xml;

// Example using DOMDocument
$data = ['name' => 'John', 'age' => 30];
$doc = new DOMDocument();
$root = $doc->createElement('root');
$doc->appendChild($root);
array_walk_recursive($data, function($value, $key) use ($doc, $root) {
    $element = $doc->createElement($key, $value);
    $root->appendChild($element);
});
echo $doc->saveXML();