写个xml生成类
将数组传进去
生成xml文件就可以
送你个 改改
class xml {
private $data;
private $encoding='utf-8'; //XML文件编码
private $root='yeedao';
public function __construct($data=''){
$this->data=$data;
if( $this->data=='' ){
exit('请传递参数');
}
}
public function __toString(){
return $this->get_xml();
}
public function get_xml() {
$xml = "encoding}\"?>";
$xml.= "<{$this->root}>";
$xml.= $this->make_xmk($this->data);
$xml.= "{$this->root}>";
return $xml;
}
protected function make_xmk($data) {
$xml = '';
foreach ($data as $key => $value) {
//如果$key是一个数字,前面加item
(is_numeric($key)) ? $key='item'.$key : $key ;
$xml.="<{$key}>";
//如果遍历的$value还是一个array,回调
$xml.= ( is_array($value) || is_object($value)) ? $this->make_xmk($value) : $value;
$xml.="{$key}>";
}
return $xml;
}
public function touch_xml($path='',$filename=''){
$path=$_SERVER['DOCUMENT_ROOT'].$path;
if($path{strlen($path)-1} != '/')$path.='/';
if(!is_dir($path))return '不是一个目录';
$filename=$path.$filename;
touch ($filename);
chmod($filename,0777);
if(is_writeable($filename)){
if($handle=fopen($filename,'w+')){
if(fwrite($handle,$this->get_xml()) === false){
return '文件'.$filename.'写入失败';
}else{
return $filename; //返回路径
}
}else{
return '不能打开文件'.$filename;
}
}else{
return '文件'.$filename.'不可写';
}
}
}
?>