xml字符串,xml對象,數組之間的相互轉化


 
         
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$arr['one'] ='你好';
$arr['two'] = 'hi';
$arr['three'] = 'hello';
$arr['four'] = 'ahan';

dump($arr);

//數組轉換成xml字符串 ,$cur_xml字符串
$curl_xml = xml_encode($arr, 'param');
dump($curl_xml);

//xml字符串轉化成xml對象, $xml對象
$xml = simplexml_load_string($curl_xml);
dump($xml);

//xml對象裝換成xml字符串, 字符串$str
$str = $xml->asXML();
dump($str);

//xml對象轉化成數組 $shuzu數組
$shuzu = json_decode(json_encode($xml),TRUE);
dump($shuzu);
}
}
 
simplexml_load_string():從xml字符串獲取SimpleXMLElement對象(函數把XML字符串載入對象中)如果失敗,則返回false
simplexml_load_file(string,class,options,ns,is_prefix);string是必須填寫,規定使用的XML字符串,class可選,規定新對象的class,兩個方法大同小異一個是獲取字符串,另一個是獲取文件
if(file_exists('../Common/timesmap.xml'))
{
  $xml = '../Common/timesmap.xml';
  $str = file_get_contents($xml);//將xml文件轉化成xml字符串
}
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM