After parsing the XML content, a corresponding array object is returned, and the return type can be set through parameter settings [array, JSON] Default: array
Because it is based on simplexml_load_string, it is not very good at parsing XML with namespace and will lose the namespace content. The above is the conclusion obtained in the test. Subsequent versions will solve this problem.
这个可以满足一般的使用需求
- function toJSON()
- {
- require_once '../classes/XmlToArray.php';
- $XML=<<
- "-//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=<<
- "-//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
- )
-
- )
-
- )
-
- )
-
- )
-
- )
-
复制代码
|