1、XML 转数组:
function xmlToArray($xml,$isfile=false){ //禁止引用外部xml实体 libxml_disable_entity_loader(true); if($isfile){ if(!file_exists($xml)) return false; $xmlstr = file_get_contents($xml); }else{ $xmlstr = $xml; } $result= json_decode(json_encode(simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $result; }
2、数组转 XML :
function arrayToXml($data){ if(!is_array($data) || count($data) <= 0){ return false; } $xml = "<xml>"; foreach ($data as $key=>$val){ if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; }
Enjoy it !