xml
// xml编码
function xml_encode($data,$encoding='utf-8',$root="data") {
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>\r\n";
$xml.= "<{$root}>\r\n";
$xml.= data_to_xml($data);
$xml.= "</{$root}>\r\n";
return $xml;
}
给你个函数,直接去用吧
function data_to_xml($data, $level = 1) {
if(is_object($data)) {
$data = get_object_vars($data);
}
$space = str_repeat("\t", $level);
$xml = '';
foreach($data as $key=>$val) {
is_numeric($key) && $key="rows";
$xml .= $space . "<$key>";
$xml .= $level == 1 ? "\r\n" : '';
$xml.=(is_array($val)||is_object($val))?data_to_xml($val, $level + 1) : $val;
list($key,)=explode(' ',$key);
$xml .= ($level == 1 ? $space : ''). "</$key>\r\n";
}
return $xml;
}
function xml_encode($data,$encoding='utf-8',$root="data") {
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>\r\n";
$xml.= "<{$root}>\r\n";
$xml.= data_to_xml($data);
$xml.= "</{$root}>\r\n";
return $xml;
}
给你个函数,直接去用吧
function data_to_xml($data, $level = 1) {
if(is_object($data)) {
$data = get_object_vars($data);
}
$space = str_repeat("\t", $level);
$xml = '';
foreach($data as $key=>$val) {
is_numeric($key) && $key="rows";
$xml .= $space . "<$key>";
$xml .= $level == 1 ? "\r\n" : '';
$xml.=(is_array($val)||is_object($val))?data_to_xml($val, $level + 1) : $val;
list($key,)=explode(' ',$key);
$xml .= ($level == 1 ? $space : ''). "</$key>\r\n";
}
return $xml;
}