0

0

XML 转成 数组对象

php中文网

php中文网

发布时间:2016-07-25 09:05:07

|

1340人浏览过

|

来源于php中文网

原创

将XML内容解析后返回一个对应的数组对象,并且可以通过参数设置来设置返回类型【数组、JSON】 默认:数组
由于是基于simplexml_load_string 对带有命名空间的XML解析不是很好,会丢失命名空间内容 以上是在测试中得到的结论,后续版本会解决这个问题。
这个可以满足一般的使用需求
  1. function toJSON()
  2. {
  3. require_once '../classes/XmlToArray.php';
  4. $XML=
  5. li> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  6. "http://struts.apache.org/dtds/struts-2.0.dtd">
  7. add.action
  8. /emp/add_suc.jsp
  9. /emp/list.jsp
  10. delete.action
  11. /emp/delete_suc.jsp
  12. update.action
  13. /emp/edit_suc.jsp
  14. /emp/edit.jsp
  15. XML;
  16. header("Content-type: text/html; charset=utf-8") ;
  17. $xml_to_array = new XmlToArray();
  18. $xml_to_array->setXml($XML);
  19. // 当标签名与内置属性有冲突的时候可以自定义相关属性名,一般其概况不需要设置
  20. //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
  21. $r = $xml_to_array->parseXml(true);
  22. print_r( $r ) ;
  23. }
  24. // 打印结果:
  25. {"struts":{"attributes":[],"content":"","constant":{"attributes":{"name":"struts.objectFactory","value":"spring"},"content":""},"package":{"attributes":{"name":"crm_employee","extends":"struts-default","namespace":"\/emp"},"content":"","action":[{"attributes":{"name":"add","class":"addBean","method":"add"},"content":"","result":[{"attributes":[],"content":"add.action"},{"attributes":[],"content":"\/emp\/add_suc.jsp"}]},{"attributes":{"name":"list","class":"listBean","method":"list"},"content":"","result":{"attributes":[],"content":"\/emp\/list.jsp"}},{"attributes":{"name":"delete","class":"deleteBean","method":"delete"},"content":"","result":{"attributes":[],"content":"\/emp\/delete_suc.jsp"}},{"attributes":{"name":"update","class":"updateBean","method":"update"},"content":"","result":{"attributes":[],"content":"\/emp\/edit_suc.jsp"}},{"attributes":{"name":"edit","class":"editBean","method":"edit"},"content":"","result":{"attributes":[],"content":"\/emp\/edit.jsp"}}]}}}
  26. // 打印数组
  27. function toArray()
  28. {
  29. require_once '../classes/XmlToArray.php';
  30. $XML=
  31. li> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  32. "http://struts.apache.org/dtds/struts-2.0.dtd">
  33. add.action
  34. /emp/add_suc.jsp
  35. /emp/list.jsp
  36. delete.action
  37. /emp/delete_suc.jsp
  38. update.action
  39. /emp/edit_suc.jsp
  40. /emp/edit.jsp
  41. XML;
  42. header("Content-type: text/html; charset=utf-8") ;
  43. $xml_to_array = new XmlToArray();
  44. $xml_to_array->setXml($XML);
  45. // 当标签名与内置属性有冲突的时候可以自定义相关属性名,一般其概况不需要设置
  46. //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
  47. $r = $xml_to_array->parseXml();
  48. print_r( $r ) ;
  49. }
  50. // 打印结果
  51. Array
  52. (
  53. [struts] => Array
  54. (
  55. [attributes] => Array
  56. (
  57. )
  58. [content] =>
  59. [constant] => Array
  60. (
  61. [attributes] => Array
  62. (
  63. [name] => struts.objectFactory
  64. [value] => spring
  65. )
  66. [content] =>
  67. )
  68. [package] => Array
  69. (
  70. [attributes] => Array
  71. (
  72. [name] => crm_employee
  73. [extends] => struts-default
  74. [namespace] => /emp
  75. )
  76. [content] =>
  77. [action] => Array
  78. (
  79. [0] => Array
  80. (
  81. [attributes] => Array
  82. (
  83. [name] => add
  84. [class] => addBean
  85. [method] => add
  86. )
  87. [content] =>
  88. [result] => Array
  89. (
  90. [0] => Array
  91. (
  92. [attributes] => Array
  93. (
  94. )
  95. [content] => add.action
  96. )
  97. [1] => Array
  98. (
  99. [attributes] => Array
  100. (
  101. )
  102. [content] => /emp/add_suc.jsp
  103. )
  104. )
  105. )
  106. [1] => Array
  107. (
  108. [attributes] => Array
  109. (
  110. [name] => list
  111. [class] => listBean
  112. [method] => list
  113. )
  114. [content] =>
  115. [result] => Array
  116. (
  117. [attributes] => Array
  118. (
  119. )
  120. [content] => /emp/list.jsp
  121. )
  122. )
  123. [2] => Array
  124. (
  125. [attributes] => Array
  126. (
  127. [name] => delete
  128. [class] => deleteBean
  129. [method] => delete
  130. )
  131. [content] =>
  132. [result] => Array
  133. (
  134. [attributes] => Array
  135. (
  136. )
  137. [content] => /emp/delete_suc.jsp
  138. )
  139. )
  140. [3] => Array
  141. (
  142. [attributes] => Array
  143. (
  144. [name] => update
  145. [class] => updateBean
  146. [method] => update
  147. )
  148. [content] =>
  149. [result] => Array
  150. (
  151. [attributes] => Array
  152. (
  153. )
  154. [content] => /emp/edit_suc.jsp
  155. )
  156. )
  157. [4] => Array
  158. (
  159. [attributes] => Array
  160. (
  161. [name] => edit
  162. [class] => editBean
  163. [method] => edit
  164. )
  165. [content] =>
  166. [result] => Array
  167. (
  168. [attributes] => Array
  169. (
  170. )
  171. [content] => /emp/edit.jsp
  172. )
  173. )
  174. )
  175. )
  176. )
  177. )
复制代码
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: hedgehog
  4. * Date: 12-5-9
  5. * Time: 下午4:37
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. class XmlToArray
  9. {
  10. private $xml;
  11. private $contentAsName="content" ;
  12. private $attributesAsName="attributes";
  13. private $xml_array = array();
  14. public function setXml( $xmlstr )
  15. {
  16. $this->xml = $xmlstr ;
  17. return $this ;
  18. }
  19. public function setContentAsName( $name )
  20. {
  21. $this->contentAsName = $name ;
  22. return $this ;
  23. }
  24. public function setAttributeAsName( $name )
  25. {
  26. $this->attributesAsName = $name ;
  27. return $this ;
  28. }
  29. private function createXMLArray( $node,&$parent_node,$node_index =0)
  30. {
  31. $node_attrbutes= array() ;
  32. $node_name = $node->getName() ;
  33. $attributes = $node->attributes() ;
  34. $children = $node->children () ;
  35. // 遍历节点上的所有属性
  36. foreach( $attributes as $attrname => $attrvalue )
  37. {
  38. $attrvalue = ( string )$attrvalue ;
  39. $node_attrbutes[ $attrname ] = trim( $attrvalue ) ;
  40. }
  41. $content = "";
  42. if( count($children) == 0 )
  43. {
  44. $content = ( string ) $node ;
  45. }
  46. $node_array = array(
  47. $this->attributesAsName =>$node_attrbutes ,
  48. $this->contentAsName => trim( $content )
  49. );
  50. // 设置层级关系
  51. if( !isset( $parent_node[ $node_name ] ) )
  52. {
  53. $is = count( $parent_node ) ;
  54. if( !isset( $parent_node[ $this->attributesAsName ] ) && count( $parent_node ) > 0 )
  55. {
  56. $last_index = count( $parent_node ) -1 ;
  57. $parent_node =& $parent_node[ $last_index ];
  58. $parent_node[ $node_name ] = $node_array ;
  59. }
  60. else
  61. {
  62. $parent_node[ $node_name ] = $node_array ;
  63. }
  64. }
  65. else
  66. {
  67. $append = &$parent_node[ $node_name ] ;
  68. if( isset( $append[ $this->attributesAsName ] ) )
  69. {
  70. $parent_node[ $node_name ] = array( $append );
  71. $append = &$parent_node[ $node_name ] ;
  72. }
  73. if( isset( $append[ $node_index ] ) )
  74. {
  75. $append = &$append[ $node_index ] ;
  76. }
  77. // 追加
  78. array_push( $append , $node_array ) ;
  79. }
  80. $index = 0 ;
  81. // 递归操作
  82. foreach( $children as $childnode )
  83. {
  84. $parent = &$parent_node[ $node_name ] ;
  85. $this->createXMLArray( $childnode ,$parent,$index ++ );
  86. }
  87. return $parent_node ;
  88. }
  89. public function parseXml( $isjson=false)
  90. {
  91. $root = simplexml_load_string ( $this->xml ) ;
  92. $parent_node = array();
  93. $array = $this->createXMLArray( $root ,$parent_node ) ;
  94. return $isjson ? json_encode( $array ) : $array ;
  95. }
  96. }
复制代码


热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
MongoDB 教程
MongoDB 教程

共17课时 | 2.5万人学习

550W粉丝大佬手把手从零学JavaScript
550W粉丝大佬手把手从零学JavaScript

共1课时 | 0.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号