将XML内容解析后返回一个对应的数组对象,并且可以通过参数设置来设置返回类型【数组、JSON】 默认:数组
由于是基于simplexml_load_string 对带有命名空间的XML解析不是很好,会丢失命名空间内容 以上是在测试中得到的结论,后续版本会解决这个问题。
这个可以满足一般的使用需求
- function toJSON()
- {
- require_once '../classes/XmlToArray.php';
- $XML=
- 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","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"}}]}}}
- // 打印数组
- function toArray()
- {
- require_once '../classes/XmlToArray.php';
- $XML=
- 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();
- print_r( $r ) ;
- }
- // 打印结果
-
- Array
- (
- [struts] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] =>
- [constant] => Array
- (
- [attributes] => Array
- (
- [name] => struts.objectFactory
- [value] => spring
- )
-
- [content] =>
- )
-
- [package] => Array
- (
- [attributes] => Array
- (
- [name] => crm_employee
- [extends] => struts-default
- [namespace] => /emp
- )
-
- [content] =>
- [action] => Array
- (
- [0] => Array
- (
- [attributes] => Array
- (
- [name] => add
- [class] => addBean
- [method] => add
- )
-
- [content] =>
- [result] => Array
- (
- [0] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] => add.action
- )
-
- [1] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] => /emp/add_suc.jsp
- )
-
- )
-
- )
-
- [1] => Array
- (
- [attributes] => Array
- (
- [name] => list
- [class] => listBean
- [method] => list
- )
-
- [content] =>
- [result] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] => /emp/list.jsp
- )
-
- )
-
- [2] => Array
- (
- [attributes] => Array
- (
- [name] => delete
- [class] => deleteBean
- [method] => delete
- )
-
- [content] =>
- [result] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] => /emp/delete_suc.jsp
- )
-
- )
-
- [3] => Array
- (
- [attributes] => Array
- (
- [name] => update
- [class] => updateBean
- [method] => update
- )
-
- [content] =>
- [result] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] => /emp/edit_suc.jsp
- )
-
- )
-
- [4] => Array
- (
- [attributes] => Array
- (
- [name] => edit
- [class] => editBean
- [method] => edit
- )
-
- [content] =>
- [result] => Array
- (
- [attributes] => Array
- (
- )
-
- [content] => /emp/edit.jsp
- )
-
- )
-
- )
-
- )
-
- )
-
- )
-
复制代码
|