PHP中怎樣把JSON文件轉(zhuǎn)為XML格式?針對這個問題,今天小編總結(jié)這篇有關(guān)格式的文章,希望幫助更多正在學(xué)習(xí)PHP的同學(xué)。
首先獲取JSON文件中的字符;
$content = file_get_contents('./data.json');
然后使用函數(shù)“json_decode()”函數(shù)將字符串轉(zhuǎn)為數(shù)組;
$content = file_get_contents('./data.json'); $data = json_decode($content, true);
再將數(shù)組循環(huán)拼接成XML數(shù)據(jù);
$content = file_get_contents('./data.json'); $data = json_decode($content, true); function xml_encode($data) { $string=""; foreach($data as $k => $v){ $string .= "<" . $k . ">"; //判斷是否是數(shù)組,或者,對像 if(is_array($v) || is_object($v)){ //是數(shù)組或者對像就的遞歸調(diào)用 $string .= xml_encode($v); }else{ //取得標(biāo)簽數(shù)據(jù) $string .=$v; } $string .= "</" . $k . ">"; } return $string; } $content = xml_encode($data);
最后將XML數(shù)據(jù)寫入文件中并將后綴名改為“xml”即可。
$content = file_get_contents('./data.json'); $data = json_decode($content, true); function xml_encode($data) { $string=""; foreach($data as $k => $v){ $string .= "<" . $k . ">"; //判斷是否是數(shù)組,或者,對像 if(is_array($v) || is_object($v)){ //是數(shù)組或者對像就的遞歸調(diào)用 $string .= xml_encode($v); }else{ //取得標(biāo)簽數(shù)據(jù) $string .=$v; } $string .= "</" . $k . ">"; } return $string; } $content = xml_encode($data); file_put_contents('./data.xml', $content);
看完上述內(nèi)容,你們掌握將JSON文件轉(zhuǎn)為XML格式的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章標(biāo)題:PHP中怎樣把JSON文件轉(zhuǎn)為XML格式?-創(chuàng)新互聯(lián)
鏈接URL:http://www.chinadenli.net/article48/ceechp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、用戶體驗(yàn)、電子商務(wù)、定制網(wǎng)站、網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容