php--xml转json

小咪咪 2022-07-13 12:55 531阅读 0赞
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>页面编辑</title>
  6. <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/themes/default/easyui.css"/>
  7. <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/themes/icon.css"/>
  8. <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/demo/demo.css"/>
  9. <link rel="stylesheet" type="text/css" href="../css/index.css"/>
  10. <link rel="stylesheet" type="text/css" href="../css/easyui-change.css"/>
  11. <script type="text/javascript" src="../jquery-easyui-1.4.5/jquery.min.js"></script>
  12. <script type="text/javascript" src="../jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
  13. <script type="text/javascript" src="../jsPackage/datagrid-groupview.js"></script>
  14. <script type="text/javascript" src="../jsPackage/datagrid-cellediting.js"></script>
  15. <script type="text/javascript" src="../JS/init/html_init.js"></script>
  16. <script type="text/javascript" src="../JS/topPanel/file_operations.js"></script>
  17. <script type="text/javascript" src="../JS/topPanel/edit_operations.js"></script>
  18. <script type="text/javascript" src="../JS/multipleUse/module_load.js"></script>
  19. <script type="text/javascript" src="../JS/rightPanel/output_tree_data.js"></script>
  20. </head>
  21. <body>
  22. <div id="topPanel" class="topPanel">
  23. <div class="easyui-panel" style="padding:5px;">
  24. <a href="#" class="easyui-menubutton" data-options="menu:'#mm0'">文件</a>
  25. <a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'icon-edit'">编辑</a>
  26. </div>
  27. <div id="mm0" style="width:150px;">
  28. <div id="menubutton_file_add" data-options="iconCls:'icon-add'">新建</div>
  29. <div class="menu-sep"></div>
  30. <div id="menubutton_file_open" data-options="iconCls:'icon-redo'">打开</div>
  31. <div class="menu-sep"></div>
  32. <div id="menubutton_file_save" data-options="iconCls:'icon-save'">存储</div>
  33. <div class="menu-sep"></div>
  34. <div id="menubutton_file_saveIN" data-options="iconCls:'icon-undo'">存储为</div>
  35. <div id="menubutton_file_out" class="menu-sep"></div>
  36. <div data-options="">退出</div>
  37. </div>
  38. <div id="mm1" style="width:150px;">
  39. <div>
  40. <span>插入JS脚本</span>
  41. <div>
  42. <div id="menubutton_edit_elementBefore">元素之前</div>
  43. <div class="menu-sep"></div>
  44. <div id="menubutton_edit_elementAfter">元素之后</div>
  45. </div>
  46. </div>
  47. <div class="menu-sep"></div>
  48. <div id="menubutton_edit_style">样式</div>
  49. <div class="menu-sep"></div>
  50. <div id="menubutton_edit_debug" data-options="iconCls:'icon-tip'">调试</div>
  51. </div>
  52. </div>
  53. <div id="leftPanel" class="leftPanel"></div>
  54. <div id="centerPanel" class="centerPanel">
  55. <!--
  56. <iframe id="centerShow" src="../show/show.html" style="width:100%;height:100%;overflow:auto;"></iframe>
  57. -->
  58. <div style="margin-top:50px;margin-left:50px;"><font size="4" color="blue">欢迎进入web页面编辑器</font></div>
  59. </div>
  60. <div id="rightPanel" class="rightPanel">
  61. <div id="edit" style="display:none">
  62. <div>
  63. <ul id="rightTreePanel" class="rightTreePanel"></ul>
  64. <div id="rightTree_menu"></div>
  65. </div>
  66. <div>
  67. <div id="rightDataGridPanel" class="rightDataGridPanel"></div>
  68. <div style="margin-top:2px">
  69. <input type="text" name="searchProperties" id="searchProperties"/>
  70. <font>自动刷新</font>
  71. <div id="autoRefresh_switchBtn"></div>
  72. <span id="handRefresh"></span>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. <div id="bottomPanel" class="bottomPanel">
  78. <div id="bottomPanel_panel" class="easyui-panel" style="padding:5px;height:50px;overflow-y:auto;"></div>
  79. </div>
  80. <div id="win"></div>
  81. <div id="style_window">
  82. </div>
  83. </body>
  84. </html>

现在用php去解析这个xml文件,将它变为json数据

php代码:

  1. <?php
  2. // 打开文件并读取数据并关闭
  3. $fp = fopen("comp.xml", 'r');
  4. $xmldata = fread($fp, filesize("comp.xml"));
  5. fclose($fp);
  6. // 节点上的分隔符
  7. $flag = "②";
  8. // 如果节点有值,那么#text就叫做textValue属性
  9. $valueTextName = "textValue";
  10. $json = xmlStr_to_json($xmldata, $valueTextName, $flag);
  11. print_r($json);
  12. function xmlStr_to_json($xmldata, $valueTextName, $flag){
  13. // 生成xml解析器
  14. $xmlparser = xml_parser_create();
  15. // 设置xml解析器的参数
  16. // 控制在该 XML 解析器中 大小写折叠(case-folding) 是否有效。其默认值为有效。 这里填写0表示无效
  17. xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
  18. // 是否略过由白空字符组成的值。 0为false,其他都为true
  19. xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
  20. // 将xml解析器中的数据生成数据$values, 当然也可以生成index的数组, 请看标准文档
  21. xml_parse_into_struct($xmlparser,$xmldata,$values);
  22. // 释放xml解析器
  23. xml_parser_free($xmlparser);
  24. // 将每个节点都作为不同的数据,有①是为了后面好拆分
  25. $flagNum = 0;
  26. // 中间生成的数组
  27. $data = array();
  28. // 最终要生成的数组
  29. $afterCleanData = array();
  30. values_to_data($data, $values, $flagNum, $flag, $valueTextName);
  31. clean_level($data, $flag, $afterCleanData);
  32. return json_encode($afterCleanData);
  33. }
  34. // 清除中间产生的data数组中的 level 属性
  35. function clean_level($data, $flag, &$afterCleanData){
  36. foreach($data as $x => $x_value){
  37. $text = $flag."level".$flag;
  38. if(is_array($x_value)){
  39. $afterCleanData[$x] = array();
  40. clean_level($data[$x], $flag, $afterCleanData[$x]);
  41. }
  42. else{
  43. if($x != $text){
  44. $afterCleanData[$x] = $data[$x];
  45. }
  46. }
  47. }
  48. }
  49. // 从xml解析器中的values数组生成中间数组data
  50. function values_to_data(&$data, $values, &$flagNum, $flag, $valueTextName){
  51. foreach($values as $x =>$x_value){
  52. $flagNum = $flagNum + 1;
  53. $flagStr = $flag.$flagNum;
  54. // $parentText = ($x_value["tag"]).$flagStr;
  55. // echo $parentText."<br/>";
  56. $parentText = "";
  57. findParentText($data,$x_value,$parentText,$flag);
  58. if($x_value["type"] == "complete" || $x_value["type"] == "open"){
  59. // echo $flagNum."<br/>";
  60. pushNode($data,$x_value,$parentText, $flagStr, $valueTextName, $flag);
  61. }
  62. }
  63. }
  64. // 查找父类节点
  65. function findParentText($data,$nowNode,&$parentText,$flag){
  66. foreach($data as $x =>$x_value){
  67. if(is_array($x_value)){
  68. $text = $flag."level".$flag;
  69. if($nowNode['level']>$data[$x][$text]){
  70. $parentText=$x;
  71. // 要不断的遍历下去才能找到最接近nowNode节点的父类
  72. findParentText($data[$x],$nowNode,$parentText,$flag);
  73. }
  74. }
  75. }
  76. }
  77. // 向新的data数组中插入节点数据
  78. function pushNode(&$data, $nowNode, $parentText, $flagStr, $valueTextName, $flag){
  79. // 因为最开始的时候是没有parentText的
  80. if($parentText == null || $parentText == ""){
  81. $nodeArray=array();
  82. $text = $flag."level".$flag;
  83. $nodeArray[$text] = $nowNode["level"];
  84. // 获取节点值
  85. if(isset($nowNode["value"])){
  86. $nodeArray[$valueTextName] = $nowNode["value"];
  87. }
  88. // 获取节点属性
  89. if( isset($nowNode["attributes"]) && is_array($nowNode["attributes"])){
  90. foreach($nowNode["attributes"] as $x1 => $x1_value1){
  91. $nodeArray[$x1] = $x1_value1;
  92. }
  93. }
  94. $text = $nowNode["tag"].$flagStr;
  95. $data[$text]=$nodeArray;
  96. }
  97. else{
  98. foreach($data as $x =>$x_value){
  99. if($x==$parentText){
  100. $nodeArray=array();
  101. $text = $flag."level".$flag;
  102. $nodeArray[$text] = $nowNode["level"];
  103. // 获取节点值
  104. if(isset($nowNode["value"])){
  105. $nodeArray[$valueTextName] = $nowNode["value"];
  106. }
  107. // 获取节点属性
  108. if(isset($nowNode["attributes"])){
  109. foreach($nowNode["attributes"] as $x1 => $x1_value1){
  110. $nodeArray[$x1] = $x1_value1;
  111. }
  112. }
  113. $text = $nowNode["tag"].$flagStr;
  114. $data[$x][$text]=$nodeArray;
  115. }
  116. else{
  117. if(is_array($data[$x])){
  118. pushNode($data[$x],$nowNode,$parentText,$flagStr,$valueTextName, $flag);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. ?>

代码运行结果:

  1. var a = {
  2. "html\u24611" : {
  3. "head\u24612" : {
  4. "meta\u24613" : {
  5. "http-equiv" : "Content-Type",
  6. "content" : "text\/html; charset=utf-8"
  7. },
  8. "title\u24614" : {
  9. "textValue" : "\u9875\u9762\u7f16\u8f91"
  10. },
  11. "link\u24615" : {
  12. "rel" : "stylesheet",
  13. "type" : "text\/css",
  14. "href" : "..\/jquery-easyui-1.4.5\/themes\/default\/easyui.css"
  15. },
  16. "link\u24616" : {
  17. "rel" : "stylesheet",
  18. "type" : "text\/css",
  19. "href" : "..\/jquery-easyui-1.4.5\/themes\/icon.css"
  20. },
  21. "link\u24617" : {
  22. "rel" : "stylesheet",
  23. "type" : "text\/css",
  24. "href" : "..\/jquery-easyui-1.4.5\/demo\/demo.css"
  25. },
  26. "link\u24618" : {
  27. "rel" : "stylesheet",
  28. "type" : "text\/css",
  29. "href" : "..\/css\/index.css"
  30. },
  31. "link\u24619" : {
  32. "rel" : "stylesheet",
  33. "type" : "text\/css",
  34. "href" : "..\/css\/easyui-change.css"
  35. },
  36. "script\u246110" : {
  37. "type" : "text\/javascript",
  38. "src" : "..\/jquery-easyui-1.4.5\/jquery.min.js"
  39. },
  40. "script\u246111" : {
  41. "type" : "text\/javascript",
  42. "src" : "..\/jquery-easyui-1.4.5\/jquery.easyui.min.js"
  43. },
  44. "script\u246112" : {
  45. "type" : "text\/javascript",
  46. "src" : "..\/jsPackage\/datagrid-groupview.js"
  47. },
  48. "script\u246113" : {
  49. "type" : "text\/javascript",
  50. "src" : "..\/jsPackage\/datagrid-cellediting.js"
  51. },
  52. "script\u246114" : {
  53. "type" : "text\/javascript",
  54. "src" : "..\/JS\/init\/html_init.js"
  55. },
  56. "script\u246115" : {
  57. "type" : "text\/javascript",
  58. "src" : "..\/JS\/topPanel\/file_operations.js"
  59. },
  60. "script\u246116" : {
  61. "type" : "text\/javascript",
  62. "src" : "..\/JS\/topPanel\/edit_operations.js"
  63. },
  64. "script\u246117" : {
  65. "type" : "text\/javascript",
  66. "src" : "..\/JS\/multipleUse\/module_load.js"
  67. },
  68. "script\u246118" : {
  69. "type" : "text\/javascript",
  70. "src" : "..\/JS\/rightPanel\/output_tree_data.js"
  71. }
  72. },
  73. "body\u246120" : {
  74. "div\u246121" : {
  75. "id" : "topPanel",
  76. "class" : "topPanel",
  77. "div\u246122" : {
  78. "class" : "easyui-panel",
  79. "style" : "padding:5px;",
  80. "a\u246123" : {
  81. "textValue" : "\u6587\u4ef6",
  82. "href" : "#",
  83. "class" : "easyui-menubutton",
  84. "data-options" : "menu:'#mm0'"
  85. },
  86. "a\u246124" : {
  87. "textValue" : "\u7f16\u8f91",
  88. "href" : "#",
  89. "class" : "easyui-menubutton",
  90. "data-options" : "menu:'#mm1',iconCls:'icon-edit'"
  91. }
  92. },
  93. "div\u246126" : {
  94. "id" : "mm0",
  95. "style" : "width:150px;",
  96. "div\u246127" : {
  97. "textValue" : "\u65b0\u5efa",
  98. "id" : "menubutton_file_add",
  99. "data-options" : "iconCls:'icon-add'"
  100. },
  101. "div\u246128" : {
  102. "class" : "menu-sep"
  103. },
  104. "div\u246129" : {
  105. "textValue" : "\u6253\u5f00",
  106. "id" : "menubutton_file_open",
  107. "data-options" : "iconCls:'icon-redo'"
  108. },
  109. "div\u246130" : {
  110. "class" : "menu-sep"
  111. },
  112. "div\u246131" : {
  113. "textValue" : "\u5b58\u50a8",
  114. "id" : "menubutton_file_save",
  115. "data-options" : "iconCls:'icon-save'"
  116. },
  117. "div\u246132" : {
  118. "class" : "menu-sep"
  119. },
  120. "div\u246133" : {
  121. "textValue" : "\u5b58\u50a8\u4e3a",
  122. "id" : "menubutton_file_saveIN",
  123. "data-options" : "iconCls:'icon-undo'"
  124. },
  125. "div\u246134" : {
  126. "id" : "menubutton_file_out",
  127. "class" : "menu-sep"
  128. },
  129. "div\u246135" : {
  130. "textValue" : "\u9000\u51fa",
  131. "data-options" : ""
  132. }
  133. },
  134. "div\u246137" : {
  135. "id" : "mm1",
  136. "style" : "width:150px;",
  137. "div\u246138" : {
  138. "span\u246139" : {
  139. "textValue" : "\u63d2\u5165JS\u811a\u672c"
  140. },
  141. "div\u246140" : {
  142. "div\u246141" : {
  143. "textValue" : "\u5143\u7d20\u4e4b\u524d",
  144. "id" : "menubutton_edit_elementBefore"
  145. },
  146. "div\u246142" : {
  147. "class" : "menu-sep"
  148. },
  149. "div\u246143" : {
  150. "textValue" : "\u5143\u7d20\u4e4b\u540e",
  151. "id" : "menubutton_edit_elementAfter"
  152. }
  153. }
  154. },
  155. "div\u246146" : {
  156. "class" : "menu-sep"
  157. },
  158. "div\u246147" : {
  159. "textValue" : "\u6837\u5f0f",
  160. "id" : "menubutton_edit_style"
  161. },
  162. "div\u246148" : {
  163. "class" : "menu-sep"
  164. },
  165. "div\u246149" : {
  166. "textValue" : "\u8c03\u8bd5",
  167. "id" : "menubutton_edit_debug",
  168. "data-options" : "iconCls:'icon-tip'"
  169. }
  170. }
  171. },
  172. "div\u246152" : {
  173. "id" : "leftPanel",
  174. "class" : "leftPanel"
  175. },
  176. "div\u246153" : {
  177. "id" : "centerPanel",
  178. "class" : "centerPanel",
  179. "div\u246154" : {
  180. "style" : "margin-top:50px;margin-left:50px;",
  181. "font\u246155" : {
  182. "textValue" : "\u6b22\u8fce\u8fdb\u5165web\u9875\u9762\u7f16\u8f91\u5668",
  183. "size" : "4",
  184. "color" : "blue"
  185. }
  186. }
  187. },
  188. "div\u246158" : {
  189. "id" : "rightPanel",
  190. "class" : "rightPanel",
  191. "div\u246159" : {
  192. "id" : "edit",
  193. "style" : "display:none",
  194. "div\u246160" : {
  195. "ul\u246161" : {
  196. "id" : "rightTreePanel",
  197. "class" : "rightTreePanel"
  198. },
  199. "div\u246162" : {
  200. "id" : "rightTree_menu"
  201. }
  202. },
  203. "div\u246164" : {
  204. "div\u246165" : {
  205. "id" : "rightDataGridPanel",
  206. "class" : "rightDataGridPanel"
  207. },
  208. "div\u246166" : {
  209. "style" : "margin-top:2px",
  210. "input\u246167" : {
  211. "type" : "text",
  212. "name" : "searchProperties",
  213. "id" : "searchProperties"
  214. },
  215. "font\u246168" : {
  216. "textValue" : "\u81ea\u52a8\u5237\u65b0"
  217. },
  218. "div\u246169" : {
  219. "id" : "autoRefresh_switchBtn"
  220. },
  221. "span\u246170" : {
  222. "id" : "handRefresh"
  223. }
  224. }
  225. }
  226. }
  227. },
  228. "div\u246175" : {
  229. "id" : "bottomPanel",
  230. "class" : "bottomPanel",
  231. "div\u246176" : {
  232. "id" : "bottomPanel_panel",
  233. "class" : "easyui-panel",
  234. "style" : "padding:5px;height:50px;overflow-y:auto;"
  235. }
  236. },
  237. "div\u246178" : {
  238. "id" : "win"
  239. },
  240. "div\u246179" : {
  241. "id" : "style_window"
  242. }
  243. }
  244. }
  245. }

解析:

php中有很多能够解析xml的。

  1. // 生成xml解析器
  2. $xmlparser = xml_parser_create();
  3. // 设置xml解析器的参数
  4. // 控制在该 XML 解析器中 大小写折叠(case-folding) 是否有效。其默认值为有效。 这里填写0表示无效
  5. xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
  6. // 是否略过由白空字符组成的值。 0为false,其他都为true
  7. xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
  8. // 将xml解析器中的数据生成数据$values, 当然也可以生成index的数组, 请看标准文档
  9. xml_parse_into_struct($xmlparser,$xmldata,$values);
  10. // 释放xml解析器
  11. xml_parser_free($xmlparser);

如果不设置xml_parser_set_option方法的时候,则返回回来的结果所有节点名称,节点属性名称都是大写的。至于xml_parser_set_option方法中的参数介绍:

xml_parser_set_option

(PHP 4, PHP 5)

xml_parser_set_option — 为指定 XML 解析进行选项设置

说明

bool xml_parser_set_option ( resource $parser , int $option , mixed $value )

parser

指向要设置选项信息的 XML 解析器的指针。

option

要设置的选项名称。请参考下文。

value

要给选项设置的新值。

如果 parser 参数没有指向一个合法的解析器或者指定的选项无法设置,该函数将返回 FALSE,否则将会把选项设置为指定的值并返回 TRUE。

可被设置的选项如下:


XML 解析器选项





























选项常量 数据类型 描述
XML_OPTION_CASE_FOLDING integer 控制在该 XML 解析器中 大小写折叠(case-folding) 是否有效。其默认值为有效(默认为1,即true)。大小写折叠是都变成大写的意思
XML_OPTION_SKIP_TAGSTART integer 指明在一个标记名前应略过几个字符。
XML_OPTION_SKIP_WHITE integer 是否略过由白空字符组成的值。
XML_OPTION_TARGET_ENCODING string 设置该 XML 解析器所使用的目标编码(target encoding)方式。其默认值和由 xml_parser_create() 函数设置的源编码(source encoding)方式相同。支持的目标编码方式有 ISO-8859-1US-ASCII 和 UTF-8

发表评论

表情:
评论列表 (有 0 条评论,531人围观)

还没有评论,来说两句吧...

相关阅读