Class with a method to transform array or object to XML:
class MakeXML {
public static function array_obj_to_xml($data, \SimpleXMLElement &$xmlObj) {
foreach((array) $data as $key => $value) {
if(is_numeric($key)) {
$key = "n" . $key;
}
if(is_array($value) or is_object($value)) {
$subnode = $xmlObj->addChild($key);
self::array_obj_to_xml($value, $subnode);
} else {
$xmlObj->addChild($key, htmlspecialchars($value));
}
}
}
}
Example:
$object = new \stdClass();
$object->example = "Try this class";
$objxml = new \SimpleXMLElement('<API/>');
MakeXML::array_obj_to_xml($objxml, $objxml);
echo $objxml->asXML();