Nessuna descrizione

array2xml.php 4.9KB

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