欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

XML 转成 数组对象

程序员文章站 2024-01-13 11:17:40
...
将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. }
复制代码