php array(object) 与xml相互转换


private function _array_to_xml($source, $charset='utf-8'){
$array = json_decode($source);
$pre = '<?xml version="1.0" ?>';
$xml = $pre. $this->_change($array);
return $xml;
}

private function _change($source){
$str = '';
foreach ($source as $k => $v){
$str = $str."<".$k.">";

if(is_array($v) || is_object($v)){
$str = $str. $this->_change($v);
}else{
$str = $str.$v;
}
$str = $str."</".$k.">";
}
return $str;
}

public function xml_to_json($source) {
if(is_file($source)){ //传的是文件,还是xml的string的判断
$xml_array=simplexml_load_file($source);
}else{
$xml_array=simplexml_load_string($source);
}
$json = json_encode($xml_array); //php5,以及以上,如果是更早版本,請下載JSON.php
return $json;
}


自由之思想,独立之意志
原文地址:https://www.cnblogs.com/barfoo/p/3946742.html