php array to generate Xml format

The method of generating Xml when the official account is developed

    public  function arrayToXml($arr){
        $xml = "<root>";
        foreach ($arr as $key=>$val){
            if(is_array($val)){
                $xml.="<".$key.">".arrayToXml($val)."</".$key.">";
            }else{
                $xml.="<".$key.">".$val."</".$key.">";
            }
        }
        $xml.="</root>";
        return $xml;
    }

xml to array

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;  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081821&siteId=291194637