首頁 後端開發 php教程 XML 轉成 數組對象

XML 轉成 數組對象

Jul 25, 2016 am 09:05 AM

$index = 0 ;
將XML內容解析後返回一個對應的數組對象,並且可以透過參數設定來設定返回類型【數組、JSON】 預設:數組
由於是基於simplexml_load_string 對帶有命名空間的XML解析不是很好,會丟失命名空間內容 以上是在測試中得到的結論,後續版本會解決這個問題。
這個可以滿足一般的使用需求
  1. function toJSON()
  2. {
  3. require_once '../classes/XmlToArray.php';
  4. $XML= xml version="1.0"encoding="UTF-8"?>
  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. 操作>
  13. update.action結果>
  14. / emp/edit_suc.jsp結果>
  15. /emp/edit.jsp
  16. XML;
  17. header("Content-type: text/html ; charset=utf-8") ;
  18. $xml_to_array = new XmlToArray();
  19. $xml_to_array->setXml($XML);
  20. /
  21. / / 當標籤名稱與內建屬性有衝突的時候可以自訂相關屬性名,一般其概況不需要設定
  22. //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
  23. $r = $xml_to_array->parseXml( true);
  24. print_r( $r ) ;
  25. }
  26. // 列印結果:
  27. {"struts":{"attributes": [],"content":"", "constant":{"attributes":{"name":"struts.objectFactory","value":"spring"},"content":""},"package" :{"attributes":{"name" :"crm_employee","擴展":"struts-default","命名空間":"/emp"},"內容":"","操作":[{"屬性":{"名稱":"新增" ,"class":"addBean","method":"add"},"content":"","re​​sult":[{"attributes":[ ],"content":"add.action"},{ "attributes":[],"content":"/emp/add_suc.jsp"}]},{"attributes":{"name":"list" ,"class":"listBean","method":"列表"},"內容":"","結果":{"屬性":[],"內容":"/emp/list.jsp"} },{"屬性":{"名稱":"刪除","class":"deleteBean","method":"刪除"},"content":"","re​​​​sult":{"attributes ":[],"content":"/emp/delete_suc.jsp" }},{"attributes":{"name":"update","class":"updateBean","method":"update"} ,"content":"","re​​sult":{"attributes": [],"content":"/emp/edit_suc.jsp"}},{"attributes":{"name":"edit ","class":"editBean","method":"edit"},"內容":"","結果":{"屬性":[],"內容":"/emp/edit.jsp" }}]}}}
  28. // 列印備份
  29. function toArray()
  30. {
  31. require_once '../classes/XmlToArray.php';
  32. $XML=
  33. li> "-//Apache Software Foundation //DTD Struts 設定2.0//EN"
  34. "http:// struts .apache.org/dtds/struts-2.0.dtd">
  35. add.action結果>
  36. /emp/add_suc.jsp結果>
  37. /emp/list.jsp結果>
  38. ;delete.action
  39. /emp/delete_suc.jsp
  40. update.action結果>
  41. /emp/edit_suc . jsp結果>
  42. /emp/edit.jsp結果>
  43. XML;
  44. header ( “內容類型:text/html; charset=utf-8") ;
  45. $xml_to_array = new XmlToArray();
  46. $xml_to_array->setXml($XML);
  47. // 當標籤名稱與內建屬性有衝突的時候自訂屬性相關屬性名,一般其概況不需要設定
  48. //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
  49. $r = $xml_to_array->parseX( );
  50. $r = $xml_to_array->parseX( ); $r = $xml_to_array->parseX( ); print_r( $r ) ;
  51. }
  52. // 結果列印
  53. Array
  54. (
  55. [struts] => Array
  56. (
  57. [attributes] => Array
  58. (
  59. )
  60. )
  61. [內容] =>
  62. [常數] => 陣列
  63. (
  64. [屬性] => 陣列
  65. (
  66. [姓名] => struts .objectFactory
  67. [🎜> [姓名] => struts .objectFactory
  68. [值] => spring
  69. )
  70. [內容] =>
  71. )
  72. [包] => (
  73. [屬性] => 陣列
  74. (
  75. [姓名] => crm_employee
  76. [擴充] => struts-default
  77. [命名空間] => /emp
  78. )
  79. [內容] =>
  80. [操作] => 陣列
  81. (
  82. [0] => 陣列
  83. (
  84. [屬性] => Array
  85. (
  86. [姓名] => add
  87. [類別] = > addBean
  88. [方法] => add
  89. )
  90. [內容] => ;
  91. [結果] =>陣列
  92. (
  93. [0] => 陣列
  94. (
  95. [屬性] => 陣列
  96. (
  97. )
  98. [內容] => 新增.action
  99. )
  100. [1] => 陣列
  101. (
  102. [屬性] => 陣列
  103. (
  104. )
  105. [內容] => /emp/add_suc.jsp
  106. )
  107. )
  108. )
  109. [1] => Array
  110. (
  111. [屬性] => Array
  112. (
  113. [姓名] => 列表
  114. [類別] => listBean
  115. [方法] => 列表
  116. )
  117. [內容] => [結果] => 數組
  118. (
  119. [屬性] => 數組
  120. (
  121. )
  122. [內容] => /emp/list.jsp
  123. )
  124. )
  125. [2] =>; Array
  126. (
  127. [2] =>; Array
  128. (
  129. (
  130. [屬性] => Array
  131. (
  132. [名稱] => 刪除
  133. [類別] => deleteBean
  134. [方法] => 刪除
  135. )
  136. [內容] => [結果] => 陣列
  137. (
  138. [屬性] => 陣列
  139. (
  140. )
  141. [內容] => /emp/delete_suc.jsp
  142. )
  143. )
  144. [3] =>;陣列
  145. ( [屬性] => 陣列 ([姓名] =>更新
  146. [class] => updateBean
  147. [方法] =>更新
  148. )
  149. [內容] =>
  150. [結果] =>陣列
  151. (
  152. [屬性] => 陣列
  153. (
  154. )
  155. [內容] => /emp/edit_suc.jsp
  156. )
  157. )
  158. [4] => Array
  159. (
  160. [屬性] => Array
  161. (
  162. [名稱] => 編輯
  163. [類別] => editBean
  164. [方法] => 編輯
  165. )
  166. [內容] => [結果] => 陣列
  167. (
  168. [屬性] => 陣列
  169. (
  170. )
  171. [內容] => /emp/edit.jsp
  172. )
  173. )
  174. )
  175. )
  176. )
  177. )
  178. )
)
複製程式碼
  1. /**
  2. * 由 JetBrains PhpStorm 建立。
  3. * 使用者:hedgehog
  4. * 日期:12-5-9
  5. * 時間:下午 4:37
  6. * 若要變更此模板,請使用檔案 |設定 |檔案範本。
  7. */
  8. class XmlToArray
  9. {
  10. private $xml;
  11. private $contentAsName= "private $ content" ;
  12. private $attributesAsName="attributes";
  13. private $xml_array = array();
  14. public function setXml( $xmlstr )
  15. $public function setXml( $xmlstr )
  16. { return $this ;
  17. }
  18. public function setContentAsName( $name )
  19. {
  20. $this->contentAsName = $name ; return $ this ;
  21. }
  22. public function setAttributeAsName( $name )
  23. {
  24. $this->attributesAsName = $name ;
  25. return $this ;
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. 私有函數createXMLArray( $node,&$parent_node,$node_index =0)
  41. {
  42. $node_attrbutes= array() ;
  43. $node_name = $node->getName() ;
  44. $attributes = $node->attributes() ;
  45. $attributes = $node->attributes() ;
  46. $children = $node->children () ;
  47. // 遍歷節點上的所有屬性
  48. foreach( $屬性為$attrname => $attrvalue )
  49. {
  50. $attrvalue = ( string )$attrvalue ;
  51. $node_attrbutes[ $attrname ] = trim( $attrvalue ) ;
  52. }
  53. content = "";
  54. if( count($children) == 0 )
  55. {
  56. $content = ( string ) $node ;
  57. }
  58. $node_array = array(
  59. $this->attributesAsName =>$node_attrbutes ,
  60. $this->contentAsName =>; rim( $content )
  61. );
  62. // 建立關係
  63. if( !isset( $ parent_node[ $node_name ] ) )
  64. {
  65. $is = count( $parent_node ) ;
  66. if( !isset( $parent_node[ $this->attributesAsName ] ) && count( $parent_node ) > 0 )
  67. {
  68. $last_index = count( $parent_node ) -1 ;
  69. $parent_node =& $parent_node[ $last_index ];
  70. $parent_node[ $node_name ] = $node_array ;
  71. }
  72. else
  73. {
  74. $parent_node[ node_array ;
  75. }
  76. }
  77. else
  78. {
  79. $append = &$parent_node[ $node_name ] ;
  80. if( isset( $append[ $this->attributesAsName ] ) )
  81. {
  82. $parent_node[ $node_name ] = array( $append );
  83. $append = &$parent_node[ $node_name ] ;
  84. }
  85. if( isset( $ [ $node_index ] ) )
  86. {
  87. $append = &$append[ $node_index ] ;
  88. }
  89. // 追加
  90. array_push( $append , $node_array ) ;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
}
} } }
}
// 電位操作 foreach( $children as $childnode )
{
$parent = &$parent_node[ $node_name ] ;
$parent = &$parent_node[ $node_name ] ;
$ this->createXMLArray( $childnode ,$parent,$index ++ );
}
return $parent_node ; } public function parseXml( $isjson=false) public function parseXml( $isjson { $root = simplexml_load_string ( $this->xml ) ; $parent_node = array(); $array = $this->createXMLArray( $root ,$parent_node ) ; 回傳 $isjson ?
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡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冒險:如何獲得巨型種子
3 週前 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

11個最佳PHP URL縮短腳本(免費和高級)

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

Instagram API簡介

在Laravel中使用Flash會話數據 在Laravel中使用Flash會話數據 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash會話數據

構建具有Laravel後端的React應用程序:第2部分,React 構建具有Laravel後端的React應用程序:第2部分,React Mar 04, 2025 am 09:33 AM

構建具有Laravel後端的React應用程序:第2部分,React

簡化的HTTP響應在Laravel測試中模擬了 簡化的HTTP響應在Laravel測試中模擬了 Mar 12, 2025 pm 05:09 PM

簡化的HTTP響應在Laravel測試中模擬了

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

php中的捲曲:如何在REST API中使用PHP捲曲擴展

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

在Codecanyon上的12個最佳PHP聊天腳本

宣布 2025 年 PHP 形勢調查 宣布 2025 年 PHP 形勢調查 Mar 03, 2025 pm 04:20 PM

宣布 2025 年 PHP 形勢調查

See all articles