class_xml.php 2.7 KB

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