Array2XML

The Ultimate Member Array2XML class.

Defined (1)

The class is defined in the following location(s).

/core/lib/array2xml.php  
  1. class Array2XML { 
  2.  
  3. private static $xml = null; 
  4. private static $encoding = 'UTF-8'; 
  5.  
  6. /** 
  7. * Initialize the root XML node [optional] 
  8. * @param $version 
  9. * @param $encoding 
  10. * @param $format_output 
  11. */ 
  12. public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { 
  13. self::$xml = new DomDocument($version, $encoding); 
  14. self::$xml->formatOutput = $format_output; 
  15. self::$encoding = $encoding; 
  16.  
  17. /** 
  18. * Convert an Array to XML 
  19. * @param string $node_name - name of the root node to be converted 
  20. * @param array $arr - aray to be converterd 
  21. * @return DomDocument 
  22. */ 
  23. public static function &createXML($node_name, $arr=array()) { 
  24. $xml = self::getXMLRoot(); 
  25. $xml->appendChild(self::convert($node_name, $arr)); 
  26.  
  27. self::$xml = null; // clear the xml node in the class for 2nd time use. 
  28. return $xml; 
  29.  
  30. /** 
  31. * Convert an Array to XML 
  32. * @param string $node_name - name of the root node to be converted 
  33. * @param array $arr - aray to be converterd 
  34. * @return DOMNode 
  35. */ 
  36. private static function &convert($node_name, $arr=array()) { 
  37.  
  38. //print_arr($node_name); 
  39. $xml = self::getXMLRoot(); 
  40. $node = $xml->createElement($node_name); 
  41.  
  42. if(is_array($arr)) { 
  43. // get the attributes first.; 
  44. if(isset($arr['@attributes'])) { 
  45. foreach($arr['@attributes'] as $key => $value) { 
  46. if(!self::isValidTagName($key)) { 
  47. throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); 
  48. $node->setAttribute($key, self::bool2str($value)); 
  49. unset($arr['@attributes']); //remove the key from the array once done. 
  50.  
  51. // check if it has a value stored in @value, if yes store the value and return 
  52. // else check if its directly stored as string 
  53. if(isset($arr['@value'])) { 
  54. $node->appendChild($xml->createTextNode(self::bool2str($arr['@value']))); 
  55. unset($arr['@value']); //remove the key from the array once done. 
  56. //return from recursion, as a note with value cannot have child nodes. 
  57. return $node; 
  58. } else if(isset($arr['@cdata'])) { 
  59. $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); 
  60. unset($arr['@cdata']); //remove the key from the array once done. 
  61. //return from recursion, as a note with cdata cannot have child nodes. 
  62. return $node; 
  63.  
  64. //create subnodes using recursion 
  65. if(is_array($arr)) { 
  66. // recurse to get the node for that key 
  67. foreach($arr as $key=>$value) { 
  68. if(!self::isValidTagName($key)) { 
  69. throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); 
  70. if(is_array($value) && is_numeric(key($value))) { 
  71. // MORE THAN ONE NODE OF ITS KIND; 
  72. // if the new array is numeric index, means it is array of nodes of the same kind 
  73. // it should follow the parent key name 
  74. foreach($value as $k=>$v) { 
  75. $node->appendChild(self::convert($key, $v)); 
  76. } else { 
  77. // ONLY ONE NODE OF ITS KIND 
  78. $node->appendChild(self::convert($key, $value)); 
  79. unset($arr[$key]); //remove the key from the array once done. 
  80.  
  81. // after we are done with all the keys in the array (if it is one) 
  82. // we check if it has any text value, if yes, append it. 
  83. if(!is_array($arr)) { 
  84. $node->appendChild($xml->createTextNode(self::bool2str($arr))); 
  85.  
  86. return $node; 
  87.  
  88. /** 
  89. * Get the root XML node, if there isn't one, create it. 
  90. */ 
  91. private static function getXMLRoot() { 
  92. if(empty(self::$xml)) { 
  93. self::init(); 
  94. return self::$xml; 
  95.  
  96. /** 
  97. * Get string representation of boolean value 
  98. */ 
  99. private static function bool2str($v) { 
  100. //convert boolean to text value. 
  101. $v = $v === true ? 'true' : $v; 
  102. $v = $v === false ? 'false' : $v; 
  103. return $v; 
  104.  
  105. /** 
  106. * Check if the tag name or attribute name contains illegal characters 
  107. * Ref: http://www.w3.org/TR/xml/#sec-common-syn 
  108. */ 
  109. private static function isValidTagName($tag) { 
  110. $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; 
  111. return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;