What are some common methods for serializing and deserializing PHP objects into XML format?

When working with PHP objects, it is common to need to serialize them into XML format for storage or transmission. One common method for serializing PHP objects into XML is to use the built-in SimpleXMLElement class in PHP. This class allows you to easily create XML elements and attributes from PHP objects. To deserialize XML back into PHP objects, you can use the SimpleXMLElement class to parse the XML and create PHP objects from the data.

// Serialize PHP object into XML
$obj = new stdClass();
$obj->name = 'John Doe';
$obj->age = 30;

$xml = new SimpleXMLElement('<data></data>');
array_walk_recursive((array)$obj, array ($xml, 'addChild'));

echo $xml->asXML();

// Deserialize XML into PHP object
$xmlString = '<data><name>John Doe</name><age>30</age></data>';
$xml = new SimpleXMLElement($xmlString);

$json = json_encode($xml);
$array = json_decode($json,TRUE);

$obj = (object) $array;

var_dump($obj);