将XML内容解析后返回一个对应的数组对象,并且可以通过参数设置来设置返回类型【数组、JSON】 默认:数组
由于是基于simplexml_load_string 对带有命名空间的XML解析不是很好,会丢失命名空间内容 以上是在测试中得到的结论,后续版本会解决这个问题。
这个可以满足一般的使用需求
- 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
- )
- )
- )
- )
- )
- )
- /**
- * Created by JetBrains PhpStorm.
- * User: hedgehog
- * Date: 12-5-9
- * Time: 下午4:37
- * To change this template use File | Settings | File Templates.
- */
- class XmlToArray
- {
- private $xml;
- private $contentAsName="content" ;
- private $attributesAsName="attributes";
- private $xml_array = array();
- public function setXml( $xmlstr )
- {
- $this->xml = $xmlstr ;
- return $this ;
- }
- public function setContentAsName( $name )
- {
- $this->contentAsName = $name ;
- return $this ;
- }
- public function setAttributeAsName( $name )
- {
- $this->attributesAsName = $name ;
- return $this ;
- }
- private function createXMLArray( $node,&$parent_node,$node_index =0)
- {
- $node_attrbutes= array() ;
- $node_name = $node->getName() ;
- $attributes = $node->attributes() ;
- $children = $node->children () ;
- // 遍历节点上的所有属性
- foreach( $attributes as $attrname => $attrvalue )
- {
- $attrvalue = ( string )$attrvalue ;
- $node_attrbutes[ $attrname ] = trim( $attrvalue ) ;
- }
- $content = "";
- if( count($children) == 0 )
- {
- $content = ( string ) $node ;
- }
- $node_array = array(
- $this->attributesAsName =>$node_attrbutes ,
- $this->contentAsName => trim( $content )
- );
- // 设置层级关系
- if( !isset( $parent_node[ $node_name ] ) )
- {
- $is = count( $parent_node ) ;
- if( !isset( $parent_node[ $this->attributesAsName ] ) && count( $parent_node ) > 0 )
- {
- $last_index = count( $parent_node ) -1 ;
- $parent_node =& $parent_node[ $last_index ];
- $parent_node[ $node_name ] = $node_array ;
- }
- else
- {
- $parent_node[ $node_name ] = $node_array ;
- }
- }
- else
- {
- $append = &$parent_node[ $node_name ] ;
- if( isset( $append[ $this->attributesAsName ] ) )
- {
- $parent_node[ $node_name ] = array( $append );
- $append = &$parent_node[ $node_name ] ;
- }
- if( isset( $append[ $node_index ] ) )
- {
- $append = &$append[ $node_index ] ;
- }
- // 追加
- array_push( $append , $node_array ) ;
- }
- $index = 0 ;
- // 递归操作
- foreach( $children as $childnode )
- {
- $parent = &$parent_node[ $node_name ] ;
- $this->createXMLArray( $childnode ,$parent,$index ++ );
- }
- return $parent_node ;
- }
- public function parseXml( $isjson=false)
- {
- $root = simplexml_load_string ( $this->xml ) ;
- $parent_node = array();
- $array = $this->createXMLArray( $root ,$parent_node ) ;
- return $isjson ? json_encode( $array ) : $array ;
- }
- }
|