首页 > 编程语言 > 详细

xml字符串,xml对象,数组之间的相互转化

时间:2016-10-09 16:45:47      阅读:175      评论:0      收藏:0      [点我收藏+]
<?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字符串
        $curl_xml = xml_encode($arr, ‘param‘);
        dump($curl_xml);

        //xml字符串转化成xml对象
        $xml = simplexml_load_string($curl_xml);
        dump($xml);

        //xml对象装换成xml字符串
        $str = $xml->asXML();
        dump($str);

        //xml字符串转化成数组
        $attr = json_decode(json_encode($xml),TRUE);
        dump($attr);

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

xml字符串,xml对象,数组之间的相互转化

原文:http://www.cnblogs.com/Duriyya/p/5942795.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!