首页 后端开发 php教程 XML 转成 数组对象

XML 转成 数组对象

Jul 25, 2016 am 09:05 AM

将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. }
复制代码


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

长URL(通常用关键字和跟踪参数都混乱)可以阻止访问者。 URL缩短脚本提供了解决方案,创建了简洁的链接,非常适合社交媒体和其他平台。 这些脚本对于单个网站很有价值

Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

在Facebook在2012年通过Facebook备受瞩目的收购之后,Instagram采用了两套API供第三方使用。这些是Instagram Graph API和Instagram Basic Display API。作为开发人员建立一个需要信息的应用程序

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

这是有关用Laravel后端构建React应用程序的系列的第二个也是最后一部分。在该系列的第一部分中,我们使用Laravel为基本的产品上市应用程序创建了一个RESTFUL API。在本教程中,我们将成为开发人员

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

2025年的PHP景观调查调查了当前的PHP发展趋势。 它探讨了框架用法,部署方法和挑战,旨在为开发人员和企业提供见解。 该调查预计现代PHP Versio的增长

See all articles