XML.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: xml.class.php 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. function xml_unserialize(&$xml, $isnormal = FALSE)
  8. {
  9. $xml_parser = new XML($isnormal);
  10. $data = $xml_parser->parse($xml);
  11. $xml_parser->destruct();
  12. return $data;
  13. }
  14. function xml_serialize($arr, $htmlon = FALSE, $isnormal = FALSE, $level = 1)
  15. {
  16. $s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
  17. $space = str_repeat("\t", $level);
  18. foreach ($arr as $k => $v)
  19. {
  20. if (!is_array($v))
  21. {
  22. $s .= $space . "<item id=\"$k\">" . ($htmlon ? '<![CDATA[' : '') . $v . ($htmlon ? ']]>' : '') . "</item>\r\n";
  23. }
  24. else
  25. {
  26. $s .= $space . "<item id=\"$k\">\r\n" . xml_serialize($v, $htmlon, $isnormal, $level + 1) . $space . "</item>\r\n";
  27. }
  28. }
  29. $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
  30. return $level == 1 ? $s . "</root>" : $s;
  31. }
  32. class XML
  33. {
  34. var $parser;
  35. var $document;
  36. var $stack;
  37. var $data;
  38. var $last_opened_tag;
  39. var $isnormal;
  40. var $attrs = array();
  41. var $failed = FALSE;
  42. function __construct($isnormal)
  43. {
  44. $this->XML($isnormal);
  45. }
  46. function XML($isnormal)
  47. {
  48. $this->isnormal = $isnormal;
  49. $this->parser = xml_parser_create('ISO-8859-1');
  50. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  51. xml_set_object($this->parser, $this);
  52. xml_set_element_handler($this->parser, 'open', 'close');
  53. xml_set_character_data_handler($this->parser, 'data');
  54. }
  55. function destruct()
  56. {
  57. xml_parser_free($this->parser);
  58. }
  59. function parse(&$data)
  60. {
  61. $this->document = array();
  62. $this->stack = array();
  63. return xml_parse($this->parser, $data, true) && !$this->failed ? $this->document : '';
  64. }
  65. function open(&$parser, $tag, $attributes)
  66. {
  67. $this->data = '';
  68. $this->failed = FALSE;
  69. if (!$this->isnormal)
  70. {
  71. if (isset($attributes['id']) && !is_string($this->document[$attributes['id']]))
  72. {
  73. $this->document = &$this->document[$attributes['id']];
  74. }
  75. else
  76. {
  77. $this->failed = TRUE;
  78. }
  79. }
  80. else
  81. {
  82. if (!isset($this->document[$tag]) || !is_string($this->document[$tag]))
  83. {
  84. $this->document = &$this->document[$tag];
  85. }
  86. else
  87. {
  88. $this->failed = TRUE;
  89. }
  90. }
  91. $this->stack[] = &$this->document;
  92. $this->last_opened_tag = $tag;
  93. $this->attrs = $attributes;
  94. }
  95. function data(&$parser, $data)
  96. {
  97. if ($this->last_opened_tag != NULL)
  98. {
  99. $this->data .= $data;
  100. }
  101. }
  102. function close(&$parser, $tag)
  103. {
  104. if ($this->last_opened_tag == $tag)
  105. {
  106. $this->document = $this->data;
  107. $this->last_opened_tag = NULL;
  108. }
  109. array_pop($this->stack);
  110. if ($this->stack)
  111. {
  112. $this->document = &$this->stack[count($this->stack) - 1];
  113. }
  114. }
  115. }