xml.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $xml_parser = new XML($isnormal);
  9. $data = $xml_parser->parse($xml);
  10. $xml_parser->destruct();
  11. return $data;
  12. }
  13. function xml_serialize($arr, $htmlon = FALSE, $isnormal = FALSE, $level = 1) {
  14. $s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
  15. $space = str_repeat("\t", $level);
  16. foreach($arr as $k => $v) {
  17. if(!is_array($v)) {
  18. $s .= $space."<item id=\"$k\">".($htmlon ? '<![CDATA[' : '').$v.($htmlon ? ']]>' : '')."</item>\r\n";
  19. } else {
  20. $s .= $space."<item id=\"$k\">\r\n".xml_serialize($v, $htmlon, $isnormal, $level + 1).$space."</item>\r\n";
  21. }
  22. }
  23. $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
  24. return $level == 1 ? $s."</root>" : $s;
  25. }
  26. class XML {
  27. var $parser;
  28. var $document;
  29. var $stack;
  30. var $data;
  31. var $last_opened_tag;
  32. var $isnormal;
  33. var $attrs = array();
  34. var $failed = FALSE;
  35. function __construct($isnormal) {
  36. $this->XML($isnormal);
  37. }
  38. function XML($isnormal) {
  39. $this->isnormal = $isnormal;
  40. $this->parser = xml_parser_create('ISO-8859-1');
  41. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  42. xml_set_object($this->parser, $this);
  43. xml_set_element_handler($this->parser, 'open','close');
  44. xml_set_character_data_handler($this->parser, 'data');
  45. }
  46. function destruct() {
  47. xml_parser_free($this->parser);
  48. }
  49. function parse(&$data) {
  50. $this->document = array();
  51. $this->stack = array();
  52. return xml_parse($this->parser, $data, true) && !$this->failed ? $this->document : '';
  53. }
  54. function open(&$parser, $tag, $attributes) {
  55. $this->data = '';
  56. $this->failed = FALSE;
  57. if(!$this->isnormal) {
  58. if(isset($attributes['id']) && !is_string($this->document[$attributes['id']])) {
  59. $this->document = &$this->document[$attributes['id']];
  60. } else {
  61. $this->failed = TRUE;
  62. }
  63. } else {
  64. if(!isset($this->document[$tag]) || !is_string($this->document[$tag])) {
  65. $this->document = &$this->document[$tag];
  66. } else {
  67. $this->failed = TRUE;
  68. }
  69. }
  70. $this->stack[] = &$this->document;
  71. $this->last_opened_tag = $tag;
  72. $this->attrs = $attributes;
  73. }
  74. function data(&$parser, $data) {
  75. if($this->last_opened_tag != NULL) {
  76. $this->data .= $data;
  77. }
  78. }
  79. function close(&$parser, $tag) {
  80. if($this->last_opened_tag == $tag) {
  81. $this->document = $this->data;
  82. $this->last_opened_tag = NULL;
  83. }
  84. array_pop($this->stack);
  85. if($this->stack) {
  86. $this->document = &$this->stack[count($this->stack)-1];
  87. }
  88. }
  89. }
  90. ?>