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字符串
}