xmlToArray和arrayToXml

xmlToArray方法,供参考

    public function xmlToArray($xml)
    {
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring),true);
        
        // libxml_disable_entity_loader(true);
        // $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $val;
    }

arrayToXml方法,供参考

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

  

posted @ 2019-01-15 10:33  瑛雄  阅读(1731)  评论(0编辑  收藏  举报