xml.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2009 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: db.class.php 12126 2008-01-11 09:40:32Z heyond $
  6. */
  7. function xml_unserialize(&$xml) {
  8. $xml_parser = new XML();
  9. $data = $xml_parser->parse($xml);
  10. $xml_parser->destruct();
  11. $arr = xml_format_array($data);
  12. return $arr['root'];
  13. }
  14. function xml_serialize(&$data, $htmlon = 0, $level = 1) {
  15. $space = str_repeat("\t", $level);
  16. $cdatahead = $htmlon ? '<![CDATA[' : '';
  17. $cdatafoot = $htmlon ? ']]>' : '';
  18. $s = '';
  19. if(!empty($data)) {
  20. foreach($data as $key => $val) {
  21. if(!is_array($val)) {
  22. $val = "$cdatahead$val$cdatafoot";
  23. if(is_numeric($key)) {
  24. $s .= "$space<item_$key>$val</item_$key>";
  25. } elseif($key === '') {
  26. $s .= '';
  27. } else {
  28. $s .= "$space<$key>$val</$key>";
  29. }
  30. } else {
  31. if(is_numeric($key)) {
  32. $s .= "$space<item_$key>".xml_serialize($val, $htmlon, $level+1)."$space</item_$key>";
  33. } elseif($key === '') {
  34. $s .= '';
  35. } else {
  36. $s .= "$space<$key>".xml_serialize($val, $htmlon, $level+1)."$space</$key>";
  37. }
  38. }
  39. }
  40. }
  41. $s = preg_replace("/([\x01-\x09\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
  42. return ($level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root>" : '').$s.($level == 1 ? '</root>' : '');
  43. }
  44. function xml_format_array($arr, $level = 0) {
  45. foreach((array)$arr as $key => $val) {
  46. if(is_array($val)) {
  47. $val = xml_format_array($val, $level + 1);
  48. }
  49. if(is_string($key) && strpos($key, 'item_') === 0) {
  50. $arr[intval(substr($key, 5))] = $val;
  51. unset($arr[$key]);
  52. } else {
  53. $arr[$key] = $val;
  54. }
  55. }
  56. return $arr;
  57. }
  58. class XML {
  59. var $parser;
  60. var $document;
  61. var $parent;
  62. var $stack;
  63. var $last_opened_tag;
  64. function XML() {
  65. $this->parser = xml_parser_create('ISO-8859-1');
  66. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  67. xml_set_object($this->parser, $this);
  68. xml_set_element_handler($this->parser, 'open','close');
  69. xml_set_character_data_handler($this->parser, 'data');
  70. }
  71. function destruct() {
  72. xml_parser_free($this->parser);
  73. }
  74. function parse(&$data) {
  75. $this->document = array();
  76. $this->stack = array();
  77. $this->parent = &$this->document;
  78. return xml_parse($this->parser, $data, true) ? $this->document : NULL;
  79. }
  80. function open(&$parser, $tag, $attributes) {
  81. $this->data = '';
  82. $this->last_opened_tag = $tag;
  83. if(is_array($this->parent) and array_key_exists($tag,$this->parent)) {
  84. if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])) {
  85. $key = count_numeric_items($this->parent[$tag]);
  86. }else{
  87. if(array_key_exists($tag.'_attr',$this->parent)) {
  88. $arr = array('0_attr'=>&$this->parent[$tag.'_attr'], &$this->parent[$tag]);
  89. unset($this->parent[$tag.'_attr']);
  90. } else {
  91. $arr = array(&$this->parent[$tag]);
  92. }
  93. $this->parent[$tag] = &$arr;
  94. $key = 1;
  95. }
  96. $this->parent = &$this->parent[$tag];
  97. } else {
  98. $key = $tag;
  99. }
  100. if($attributes) {
  101. $this->parent[$key.'_attr'] = $attributes;
  102. }
  103. $this->parent = &$this->parent[$key];
  104. $this->stack[] = &$this->parent;
  105. }
  106. function data(&$parser, $data) {
  107. if($this->last_opened_tag != NULL)
  108. $this->data .= $data;
  109. }
  110. function close(&$parser, $tag) {
  111. if($this->last_opened_tag == $tag) {
  112. $this->parent = $this->data;
  113. $this->last_opened_tag = NULL;
  114. }
  115. array_pop($this->stack);
  116. if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
  117. }
  118. }
  119. function count_numeric_items(&$array) {
  120. return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
  121. }
  122. ?>