將XML內容解析後返回一個對應的數組對象,並且可以透過參數設定來設定返回類型【數組、JSON】 預設:數組
由於是基於simplexml_load_string 對帶有命名空間的XML解析不是很好,會丟失命名空間內容 以上是在測試中得到的結論,後續版本會解決這個問題。
這個可以滿足一般的使用需求
- function toJSON()
- {
- require_once '../classes/XmlToArray.php';
- $XML= xml version="1.0"encoding="UTF-8"?>
- li> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http:/ /struts.apache.org/dtds/struts-2.0.dtd">
-
-
-
-
-
- add.action;
- /emp/add_suc.jsp
-
-
- /emp/list.jsp
-
-
- delete.action結果>
- /emp/delete_suc.jsp結果>
- 操作>
-
- update.action結果>
- / emp/edit_suc.jsp結果>
-
-
- /emp/edit.jsp
-
-
-
-
- XML;
-
- header("Content-type: text/html ; charset=utf-8") ;
- $xml_to_array = new XmlToArray();
- $xml_to_array->setXml($XML);
- /
- / / 當標籤名稱與內建屬性有衝突的時候可以自訂相關屬性名,一般其概況不需要設定
- //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
- $r = $xml_to_array->parseXml( true);
- print_r( $r ) ;
- }
- // 列印結果:
- {"struts":{"attributes": [],"content":"", "constant":{"attributes":{"name":"struts.objectFactory","value":"spring"},"content":""},"package" :{"attributes":{"name" :"crm_employee","擴展":"struts-default","命名空間":"/emp"},"內容":"","操作":[{"屬性":{"名稱":"新增" ,"class":"addBean","method":"add"},"content":"","result":[{"attributes":[ ],"content":"add.action"},{ "attributes":[],"content":"/emp/add_suc.jsp"}]},{"attributes":{"name":"list" ,"class":"listBean","method":"列表"},"內容":"","結果":{"屬性":[],"內容":"/emp/list.jsp"} },{"屬性":{"名稱":"刪除","class":"deleteBean","method":"刪除"},"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"},"內容":"","結果":{"屬性":[],"內容":"/emp/edit.jsp" }}]}}}
- // 列印備份
- function toArray()
- {
- require_once '../classes/XmlToArray.php';
- $XML=
- li> "-//Apache Software Foundation //DTD Struts 設定2.0//EN"
- "http:// struts .apache.org/dtds/struts-2.0.dtd">
-
-
-
-
-
- add.action結果>
- /emp/add_suc.jsp結果>
-
-
- /emp/list.jsp結果>
-
-
- ;delete.action
- /emp/delete_suc.jsp
-
-
- update.action結果>
- /emp/edit_suc . jsp結果>
-
-
- /emp/edit.jsp結果>
-
-
-
-
- XML;
-
- header ( “內容類型:text/html; charset=utf-8") ;
- $xml_to_array = new XmlToArray();
- $xml_to_array->setXml($XML);
- // 當標籤名稱與內建屬性有衝突的時候自訂屬性相關屬性名,一般其概況不需要設定
- //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
- $r = $xml_to_array->parseX( );
- $r = $xml_to_array->parseX( ); $r = $xml_to_array->parseX( ); print_r( $r ) ;
- }
- // 結果列印
-
- Array
- (
- [struts] => Array
- (
- [attributes] => Array
- (
- )
- )
-
- [內容] =>
- [常數] => 陣列
- (
- [屬性] => 陣列
- (
- [姓名] => struts .objectFactory
- [🎜> [姓名] => struts .objectFactory
- [值] => spring
- )
-
- [內容] =>
- )
-
- [包] => (
- [屬性] => 陣列
- (
- [姓名] => crm_employee
- [擴充] => struts-default
- [命名空間] => /emp
- )
-
- [內容] =>
- [操作] => 陣列
- (
- [0] => 陣列
- (
- [屬性] => Array
- (
- [姓名] => add
- [類別] = > addBean
- [方法] => add
- )
-
- [內容] => ;
- [結果] =>陣列
- (
- [0] => 陣列
- (
- [屬性] => 陣列
- (
- )
-
- [內容] => 新增.action
- )
-
- [1] => 陣列
- (
- [屬性] => 陣列
- (
- )
-
- [內容] => /emp/add_suc.jsp
- )
-
- )
-
- )
-
- [1] => Array
- (
- [屬性] => Array
- (
- [姓名] => 列表
- [類別] => listBean
- [方法] => 列表
- )
-
- [內容] => [結果] => 數組
- (
- [屬性] => 數組
- (
- )
-
- [內容] => /emp/list.jsp
- )
-
- )
-
- [2] =>; Array
- (
-
- [2] =>; Array
- (
- (
- [屬性] => Array
- (
- [名稱] => 刪除
- [類別] => deleteBean
- [方法] => 刪除
- )
-
- [內容] => [結果] => 陣列
- (
- [屬性] => 陣列
- (
- )
-
- [內容] => /emp/delete_suc.jsp
- )
-
- )
-
- [3] =>;陣列
- ( [屬性] => 陣列 ([姓名] =>更新
- [class] => updateBean
- [方法] =>更新
- )
-
- [內容] =>
- [結果] =>陣列
- (
- [屬性] => 陣列
- (
- )
-
- [內容] => /emp/edit_suc.jsp
- )
-
- )
-
- [4] => Array
- (
- [屬性] => Array
- (
- [名稱] => 編輯
- [類別] => editBean
- [方法] => 編輯
- )
-
- [內容] => [結果] => 陣列
- (
- [屬性] => 陣列
- (
- )
-
- [內容] => /emp/edit.jsp
- )
-
- )
-
- )
-
- )
-
- )
-
- )
-
- )
)
複製程式碼 } |
$index = 0 ;