xml.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author wangtiecheng(jim_@live.cn)
  4. * @link http://www.phpcms.cn http://www.ku6.cn
  5. * @copyright 2005-2009 PHPCMS/KU6 Software LLCS
  6. * @license http://www.phpcms.cn/license/
  7. * @datetime Wed Aug 05 18:37:10 CST 2009
  8. * @lastmodify Wed Aug 05 18:37:10 CST 2009
  9. * ------------------------------------------------------------
  10. * $xml = new xml();
  11. * $res = $xml->xml_unserialize($data);
  12. */
  13. class xml {
  14. var $parser;
  15. var $document;
  16. var $parent;
  17. var $stack;
  18. var $last_opened_tag;
  19. public function xml() {
  20. $this->parser = xml_parser_create();
  21. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  22. xml_set_object($this->parser, $this);
  23. xml_set_element_handler($this->parser, 'open','close');
  24. xml_set_character_data_handler($this->parser, 'data');
  25. }
  26. public function destruct() {
  27. xml_parser_free($this->parser);
  28. }
  29. /**
  30. * unserialize
  31. * @param xml字符串
  32. * @return array
  33. */
  34. public function xml_unserialize($xml) {
  35. $data = $this->parse($xml);
  36. $this->destruct();
  37. return $data;
  38. }
  39. /**
  40. * serialize
  41. * @param $data 数组
  42. * @return string
  43. */
  44. public function xml_serialize(&$data, $level = 0, $prior_key = NULL) {
  45. if($level == 0) {
  46. ob_start();
  47. echo "<?xml version=\"1.0\" encoding=\"".CHARSET."\"?>\n<root>","\n";
  48. }
  49. while(list($key, $value) = each($data)) {
  50. if(!strpos($key, ' attr')) {
  51. if(is_array($value) and array_key_exists(0, $value)) {
  52. $this->xml_serialize($value, $level, $key);
  53. } else {
  54. $tag = $prior_key ? $prior_key : (is_numeric($key) ? 'item' : $key);
  55. echo str_repeat("\t", $level),'<',$tag;
  56. if(array_key_exists("$key attr", $data)) {
  57. while(list($attr_name, $attr_value) = each($data["$key attr"])) {
  58. echo ' ',$attr_name,'="',new_html_special_chars($attr_value),'"';
  59. }
  60. reset($data["$key attr"]);
  61. }
  62. if(is_null($value)) {
  63. echo " />\n";
  64. } elseif(!is_array($value)) {
  65. echo '>',new_html_special_chars($value),"</$tag>\n";
  66. } else {
  67. echo ">\n",$this->xml_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
  68. }
  69. }
  70. }
  71. }
  72. reset($data);
  73. if($level == 0) {
  74. $str = &ob_get_contents();
  75. ob_end_clean();
  76. return $str.'</root>';
  77. }
  78. }
  79. public function parse(&$data){
  80. $this->document = array();
  81. $this->stack = array();
  82. $this->parent = &$this->document;
  83. return xml_parse($this->parser, $data, true) ? $this->document : NULL;
  84. }
  85. public function open(&$parser, $tag, $attributes){
  86. $this->data = '';
  87. $this->last_opened_tag = $tag;
  88. if(is_array($this->parent) && array_key_exists($tag, $this->parent)) {
  89. if(is_array($this->parent[$tag]) && array_key_exists(0,$this->parent[$tag])) {
  90. $key = $this->count_numeric_items($this->parent[$tag]);
  91. } else {
  92. if(array_key_exists("$tag attr", $this->parent)) {
  93. $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
  94. unset($this->parent["$tag attr"]);
  95. } else {
  96. $arr = array(&$this->parent[$tag]);
  97. }
  98. $this->parent[$tag] = &$arr;
  99. $key = 1;
  100. }
  101. $this->parent = &$this->parent[$tag];
  102. } else {
  103. $key = $tag;
  104. }
  105. if($attributes) {
  106. $this->parent["$key attr"] = $attributes;
  107. }
  108. $this->parent = &$this->parent[$key];
  109. $this->stack[] = &$this->parent;
  110. }
  111. public function data(&$parser, $data) {
  112. if($this->last_opened_tag != NULL) {
  113. $this->data .= $data;
  114. }
  115. }
  116. public function close(&$parser, $tag) {
  117. if($this->last_opened_tag == $tag) {
  118. $this->parent = $this->data;
  119. $this->last_opened_tag = NULL;
  120. }
  121. array_pop($this->stack);
  122. if($this->stack) {
  123. $this->parent = &$this->stack[count($this->stack)-1];
  124. }
  125. }
  126. public function count_numeric_items(&$array) {
  127. return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
  128. }
  129. }
  130. ?>