Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.17 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.          * The main function for converting to an XML document.
  3.          * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
  4.          *
  5.          * @param array $data
  6.          * @param SimpleXMLElement $xml - should only be used recursively
  7.          * @return string XML
  8.          */
  9.         public static function array2xml($data, $xml=null)
  10.         {
  11.                 // turn off compatibility mode as simple xml throws a wobbly if you don't.
  12.                 if (ini_get('zend.ze1_compatibility_mode') == 1)
  13.                 {
  14.                         ini_set ('zend.ze1_compatibility_mode', 0);
  15.                 }
  16.                
  17.                 $root = null;
  18.                 if ($xml == null)
  19.                 {
  20.                         $keys = array_keys($data);
  21.                         $root = $keys[0];
  22.                         $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$root/>");
  23.                        
  24.                         //Only when empty root element
  25.                         if(empty($data[$root])) {
  26.                                 return $xml->asXML();
  27.                         }
  28.                         else if(is_string($data[$root])) {
  29.                                 $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$root>".$data[$root]."</$root>");
  30.                                 return $xml->asXML();
  31.                         }
  32.                 }
  33.  
  34.                 // loop through the data passed in.
  35.                 foreach($data as $key => $value)
  36.                 {
  37.                         if(!empty($root)) {
  38.                                 if(is_array($value)) {
  39.                                         XML::array2xml($value, $xml);
  40.                                 }
  41.                         }
  42.                         else {
  43.                                 // no numeric keys in our xml please!
  44.                                 if (is_numeric($key))
  45.                                 {
  46.                                         // make string key...
  47.                                         $key = "unknownNode_". (string) $key;
  48.                                 }
  49.          
  50.                                 // replace anything not alpha numeric
  51.                                 $key = preg_replace('/[^a-z]/i', '', $key);
  52.          
  53.                                 // if there is another array found recrusively call this function
  54.                                 if (is_array($value) && !isset($value[0]))
  55.                                 {
  56.                                         $node = $xml->addChild($key);
  57.                                         // recrusive call.
  58.                                         XML::array2xml($value, $node);
  59.                                 }
  60.                                 else
  61.                                 {
  62.                                         if(!is_array($value)) {
  63.                                                 $value = htmlentities($value);
  64.                                                 $xml->addChild($key,$value);
  65.                                         }
  66.                                         else {
  67.                                                 foreach($value as $v) {
  68.                                                         if(is_array($v)) {
  69.                                                                 $node = $xml->addChild($key);
  70.                                                                 // recrusive call.
  71.                                                                 XML::array2xml($v, $node);
  72.                                                         }
  73.                                                         else {
  74.                                                                 $v = htmlentities($v);
  75.                                                                 $xml->addChild($key,$v);
  76.                                                         }
  77.                                                 }
  78.                                         }
  79.                                 }
  80.                         }
  81.  
  82.                 }
  83.                 // pass back as string. or simple xml object if you want!
  84.                 return $xml->asXML();
  85.         }