Revision: 33991
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 15, 2010 19:52 by flashmac
Initial Code
class XMLSerializer { // functions adopted from http://www.sean-barton.co.uk/2009/03/turning-an-array-or-object-into-xml-using-php/ public static function generateValidXmlFromObj($obj, $node_block='nodes', $node_name='node') { $arr = get_object_vars($obj); return self::generateValidXmlFromArray($arr, $node_block, $node_name); } public static function generateValidXmlFromArray($array, $node_block='nodes', $node_name='node') { $xml = '<?xml version="1.0" encoding="UTF-8" ?>'; $xml .= '<' . $node_block . '>'; $xml .= self::generateXmlFromArray($array, $node_name); $xml .= '</' . $node_block . '>'; return $xml; } private static function generateXmlFromArray($array, $node_name) { $xml = ''; if (is_array($array) || is_object($array)) { foreach ($array as $key=>$value) { if (is_numeric($key)) { $key = $node_name; } $xml .= '<' . $key . '>' . self::generateXmlFromArray($value, $node_name) . '</' . $key . '>'; } } else { $xml = htmlspecialchars($array, ENT_QUOTES); } return $xml; } }
Initial URL
Initial Description
Initial Title
generate XML from Object... AND... XML from Array... AND XML from Array
Initial Tags
object, array, xml, convert
Initial Language
PHP