将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 ) ) ) ) ))
复制代码
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 ; }}
复制代码