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;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。