class_xml.php 2.7 KB

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