/** * The main function for converting to an XML document. * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. * * @param array $data * @param string $rootNodeName - what you want the root node to be - defaultsto data. * @param SimpleXMLElement $xml - should only be used recursively * @return string XML */ public static function toXML($data, $rootNodeName = 'ResultSet', &$xml = null) { // turn off compatibility mode as simple xml throws a wobbly if you don't. if (ini_get('zend.ze1_compatibility_mode') == 1) { ini_set('zend.ze1_compatibility_mode', 0); } if (is_null($xml)) { $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><{$rootNodeName} />"); } //if ( is_null( $xml ) ) $xml = simplexml_load_string( "" ); // loop through the data passed in. foreach ($data as $key => $value) { // no numeric keys in our xml please! if (is_numeric($key)) { $numeric = 1; $key = $rootNodeName; } // delete any char not allowed in XML element names $key = preg_replace('/[^a-z0-9\\-\\_\\.\\:]/i', '', $key); // if there is another array found recrusively call this function if (is_array($value)) { $node = ArrayToXML::isAssoc($value) || $numeric ? $xml->addChild($key) : $xml; // recrusive call. if ($numeric) { $key = 'anon'; } ArrayToXML::toXml($value, $key, $node); } else { // add single node. $value = htmlentities($value); $xml->addChild($key, $value); } } // pass back as XML //return $xml->asXML(); // if you want the XML to be formatted, use the below instead to return the XML $doc = new DOMDocument('1.0'); $doc->preserveWhiteSpace = false; $doc->loadXML($xml->asXML()); $doc->formatOutput = true; return $doc->saveXML(); }
/** * The main function for converting to an XML document. * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. * * @static * @param array $data * @param string $rootNodeName - what you want the root node to be - defaultsto data. * @param SimpleXMLElement $xml - should only be used recursively * @return string XML */ public static function toXml($data, $rootNodeName = 'data', &$xml = NULL) { if (is_null($xml)) { $xml = new SimpleXMLElement('<' . $rootNodeName . '/>'); } // loop through the data passed in. foreach ($data as $key => $value) { // if numeric key, assume array of rootNodeName elements if (is_numeric($key)) { $key = $rootNodeName; } // Check if is attribute if ($key == ArrayToXML::attr_arr_string) { // Add attributes to node foreach ($value as $attr_name => $attr_value) { $xml->addAttribute($attr_name, $attr_value); } } else { // delete any char not allowed in XML element names $key = preg_replace('/[^a-z0-9\\-\\_\\.\\:]/i', '', $key); // if there is another array found recrusively call this function if (is_array($value)) { // create a new node unless this is an array of elements $node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml; // recrusive call - pass $key as the new rootNodeName ArrayToXML::toXml($value, $key, $node); } else { // add single node. // $value = htmlentities($value, ENT_COMPAT | ENT_XHTML, ADA_CHARSET); $new_child = $xml->addChild($key); if ($new_child !== NULL) { $node = dom_import_simplexml($new_child); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($value)); } } } } // pass back as string. or simple xml object if you want! return $xml->asXML(); }
/** * The main function for converting to an XML document. * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. * * @param array $data * @param string $rootNodeName - what you want the root node to be - defaultsto data. * @param SimpleXMLElement $xml - should only be used recursively * @return string XML */ public static function toXml($data, $rootNodeName = 'data', &$xml = null) { // turn off compatibility mode as simple xml throws a wobbly if you don't. if (ini_get('zend.ze1_compatibility_mode') == 1) { ini_set('zend.ze1_compatibility_mode', 0); } if (is_null($xml)) { $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><{$rootNodeName} />"); } // loop through the data passed in. foreach ($data as $key => $value) { // if numeric key, assume array of rootNodeName elements if (is_numeric($key)) { $key = $rootNodeName; } // delete any char not allowed in XML element names $key = preg_replace('/[^a-z0-9\\-\\_\\.\\:]/i', '', $key); // if there is another array found recrusively call this function if (is_array($value)) { // create a new node unless this is an array of elements $node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml; // recrusive call - pass $key as the new rootNodeName ArrayToXML::toXml($value, $key, $node); } else { // add single node. $value = htmlentities($value); $xml->addChild($key, $value); } } // pass back as string. or simple xml object if you want! return $xml->asXML(); }