No Description

Xml.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. *返回码定义
  4. */
  5. /* 扩展内部错误 */
  6. define("INTERNAL_ERR", -1);
  7. /* 当前模式下不允许执行该函数 */
  8. define("WRONG_MODE", 0);
  9. /* 成功 */
  10. define("SUCESS", 1);
  11. /**
  12. * 模式定义
  13. */
  14. define("READMODE", 0);
  15. define("WRITEMODE", 1);
  16. /**
  17. * @desc LtXml用于解析和生成XML文件
  18. * 使用前调用 init() 方法对类进行初始化
  19. *
  20. * LtXml提供两个公共方法 getArray() 和 getString
  21. *
  22. * getArray() 方法要求传入一个规范的xml字符串,
  23. * 返回一个格式化的数组
  24. *
  25. * getString() 方法要求传入一个格式化的数组,反
  26. * 回一个规范的xml字符串
  27. * 在使用getString() 方法时,传入的格式化数组可
  28. * 通过 createTag() 方法获得。
  29. *
  30. */
  31. class LtXml {
  32. /**
  33. * 只支持 ISO-8859-1, UTF-8 和 US-ASCII三种编码
  34. */
  35. private $_supportedEncoding = array("ISO-8859-1", "UTF-8", "US-ASCII");
  36. /**
  37. * XMLParser 操作句柄
  38. */
  39. private $_handler;
  40. /**
  41. * READMODE 0:读模式,encoding参数不生效,通过输入的string获取version和encoding(getString方法不可用)
  42. * WRITEMODE 1:写模式,按照制定的encoding和array生成string(getArray方法不可用)
  43. */
  44. public $mode;
  45. /**
  46. * 该 XML 对象的编码,ISO-8859-1, UTF-8(默认) 或 US-ASCII
  47. */
  48. public $encoding;
  49. /**
  50. * 该 XML 对象的版本,1.0(默认)
  51. */
  52. public $version;
  53. public function init($mode = 0, $encoding = "UTF-8", $version = "1.0") {
  54. $this->mode = $mode;
  55. $this->encoding = $encoding;
  56. $this->version = $version;
  57. $this->_getParser($encoding);
  58. }
  59. public function getArray($xmlString) {
  60. if (READMODE !== $this->mode) {
  61. trigger_error("LtXml is on WRITEMODE, and cannot convert XML string to array.");
  62. return WRONG_MODE;
  63. }
  64. if (0 === preg_match("/version=[\"|\']([1-9]\d*\.\d*)[\"|\']/", $xmlString, $res)) {
  65. trigger_error("Cannot find the version in this XML document.");
  66. return INTERNAL_ERR;
  67. }
  68. else {
  69. $this->version = $res[1];
  70. }
  71. if (0 === preg_match("/encoding=[\"|\'](.*?)[\"|\']/", $xmlString, $res)) {
  72. $this->encoding = "UTF-8";
  73. }
  74. else {
  75. $this->encoding = strtoupper($res[1]);
  76. }
  77. $_array = $this->_stringToArray($xmlString);
  78. if (NULL === $_array) {
  79. trigger_error("Fail to get the tag template.");
  80. return INTERNAL_ERR;
  81. }
  82. $currentArray = NULL;
  83. $openingTags = array();
  84. $array = $this->_getArrayTemplate();
  85. foreach ($_array as $tag) {
  86. $tag["tag"] = strtolower($tag["tag"]);
  87. if (isset($tag["type"]) && "close" == $tag["type"]
  88. && isset($tag["tag"]) && ! empty($tag["tag"])) {
  89. if ($openingTags[count($openingTags) - 1]["tag"] == $tag["tag"]) {
  90. unset($openingTags[count($openingTags) - 1]);
  91. }
  92. else {
  93. return -1;
  94. }
  95. }
  96. else if ((isset($tag["type"]) && "complete" == $tag["type"])
  97. || (isset($tag["type"]) && "open" == $tag["type"])
  98. && isset($tag["tag"]) && ! empty($tag["tag"])){
  99. $currentArray = $this->_getArrayTemplate();
  100. $currentArray["tag"] = $tag["tag"];
  101. $cdata = $tag["value"];
  102. $cdata = preg_replace("/^\s*/", "", $cdata);
  103. $cdata = preg_replace("/\s*$/", "", $cdata);
  104. $currentArray["cdata"] = $cdata;
  105. if (isset($tag["attributes"]) && is_array($tag["attributes"])) {
  106. foreach($tag["attributes"] as $k => $v) {
  107. $currentArray["attributes"][strtolower($k)] = $v;
  108. }
  109. }
  110. if (0 == count($openingTags)) {
  111. $openingTags[] = &$array;
  112. $openingTags[0] = $currentArray;
  113. }
  114. else {
  115. $subCount = count($openingTags[count($openingTags) - 1]["sub"]);
  116. $openingTags[count($openingTags) - 1]["sub"][$subCount] = $currentArray;
  117. $openingTags[count($openingTags)] = &$openingTags[count($openingTags) - 1]["sub"][$subCount];
  118. }
  119. if ("complete" == $tag["type"]) {
  120. unset($openingTags[count($openingTags) - 1]);
  121. }
  122. }
  123. else if (isset($tag["type"]) && "cdata" == $tag["type"]
  124. && isset($tag["tag"]) && ! empty($tag["tag"])) {
  125. if ($tag["tag"] == $openingTags[count($openingTags) - 1]["tag"]) {
  126. $cdata = $tag["value"];
  127. $cdata = preg_replace("/^\s*/", "", $cdata);
  128. $cdata = preg_replace("/\s*$/", "", $cdata);
  129. $openingTags[count($openingTags) - 1]["cdata"] .= $cdata;
  130. }
  131. else {
  132. return -2;
  133. }
  134. }
  135. }
  136. if (0 < count($openingTags)) {
  137. return -3;
  138. }
  139. return $array;
  140. }
  141. public function getString($xmlArray) {
  142. if (WRITEMODE !== $this->mode) {
  143. trigger_error("LtXml is on READMODE, and cannot convert array to string.");
  144. return WRONG_MODE;
  145. }
  146. $header = "<?xml version=\"{$this->version}\" encoding=\"{$this->encoding}\"". " ?" . ">\n";
  147. $xmlString = $header;
  148. $processingTags = array($xmlArray);
  149. while (! empty($processingTags)) {
  150. if (! isset($processingTags[count($processingTags) -1]["close"])) {
  151. $tagArray = $processingTags[count($processingTags) - 1];
  152. if (0 === $this->_isTag($tagArray)) {
  153. trigger_error("The array do not match the format.");
  154. return INTERNAL_ERR;
  155. }
  156. $processingTags[count($processingTags) -1]["close"] = "YES";
  157. $tagName = $tagArray["tag"];
  158. $tag = "<{$tagName}";
  159. foreach ($tagArray["attributes"] as $key => $value) {
  160. $tag .= " {$key}=\"{$value}\"";
  161. }
  162. if (! empty($tagArray["sub"]) || ! empty($tagArray["cdata"])) {
  163. $cdata = $this->_convertEntity($tagArray["cdata"]);
  164. $tag .= ">\n{$cdata}\n";
  165. for ($i=count($tagArray["sub"]) - 1; $i>=0; $i--) {
  166. $subArray = $tagArray["sub"][$i];
  167. $processingTags[count($processingTags)] = $subArray;
  168. }
  169. }
  170. else {
  171. $processingTags[count($processingTags) - 1]["complete"] = "YES";
  172. }
  173. }
  174. else {
  175. $tag = (isset($processingTags[count($processingTags) - 1]["complete"]))
  176. ? "/>\n"
  177. : "</{$processingTags[count($processingTags) - 1]["tag"]}>\n";
  178. unset($processingTags[count($processingTags) - 1]);
  179. }
  180. $xmlString .= $tag;
  181. }
  182. $xmlString = preg_replace("/\n\s*/", "\n", $xmlString);
  183. return $xmlString;
  184. }
  185. /**
  186. * 生成一个xml节点
  187. * @param string tag 标签名
  188. * @param string cdata 数据
  189. * @param array attr 属性列表
  190. * @param array sub 子标签列表
  191. */
  192. public function createTag($tag, $cdata = "", $attr = array(), $sub = array()) {
  193. $newTag = $this->_getArrayTemplate();
  194. if (! is_string($tag)) {
  195. trigger_error("Cannot read the tag name.");
  196. return INTERNAL_ERR;
  197. }
  198. $newTag["tag"] = $tag;
  199. $newTag["cdata"] = $cdata;
  200. $newTag["attributes"] = $attr;
  201. $newTag["sub"] = $sub;
  202. return $newTag;
  203. }
  204. /**
  205. * 释放xml_parser
  206. */
  207. public function free() {
  208. xml_parser_free($this->_handler);
  209. }
  210. private function _getParser($encoding) {
  211. if (in_array($encoding, $this->_supportedEncoding))
  212. $this->_handler = xml_parser_create($encoding);
  213. else
  214. $this->_handler = NULL;
  215. }
  216. private function _stringToArray($xmlString) {
  217. $res = xml_parse_into_struct($this->_handler, $xmlString, $array);
  218. if (1 === $res)
  219. return $array;
  220. else
  221. return NULL;
  222. }
  223. private function _convertEntity($string) {
  224. $patterns = array("/</", "/</", "/&/", "/'/", "/\"/");
  225. $replacement = array("&lt;", "&gt;", "&amp;", "&apos;", "&quot;");
  226. return preg_replace($patterns, $replacement, $string);
  227. }
  228. private function _rConvertEntity($string) {
  229. $patterns = array("/&lt;/", "/&gt;/", "/&amp;/", "/&apos;/", "/&quot;/");
  230. $replacement = array("<", "<", "&", "'", "\"");
  231. return preg_replace($patterns, $replacement, $string);
  232. }
  233. private function _getArrayTemplate() {
  234. return array("tag" => "", "attributes" => array(), "sub" => array(), "cdata" => "");
  235. }
  236. /**
  237. * 检测传入的参数是否是一个合法的tag数组
  238. * @return 0 非法
  239. * @return 1 合法
  240. */
  241. private function _isTag($tag) {
  242. if (! is_array($tag)) {
  243. return 0;
  244. }
  245. if (! isset($tag["tag"]) || ! is_string($tag["tag"]) || empty($tag["tag"])) {
  246. return 0;
  247. }
  248. if (! isset($tag["attributes"]) || ! is_array($tag["attributes"])) {
  249. return 0;
  250. }
  251. if (! isset($tag["sub"]) || ! is_array($tag["sub"])) {
  252. return 0;
  253. }
  254. if (! isset($tag["cdata"]) || ! is_string($tag["cdata"])) {
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. }