<?php 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 ; } }
//解析为json function toJSON() { require_once 'XmlToArray.php'; $XML= file_get_contents('simple.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 ) ; } // 解析为数组 function toArray() { require_once 'XmlToArray.php'; $XML = file_get_contents('simple.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 ) ; }
另外一种简单的方法,待验证
function xml2arr($xml){ $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $json = json_encode($obj); $arr = json_decode($json, true); return $arr; }
转自:http://www.qaulau.com/php-xml-to-array-class/
http://www.thinkphp.cn/code/1123.html