1.导出xml文件
$this->load->dbutil();
$r=$this->db->query($sql);
$config = array (
'root' => 'root',
'element' => 'course',
'newline' => "",
'tab' => ""
);
echo $this->dbutil->xml_from_result($r,$config);
参考;http://hi.baidu.com/eecc00/item/17d61acf06664e5ba9ba94ee php框架codeigniter数据库操作整理(16.dbutil(数据库工具类)
2.将xml转化为数组
function export_in()
{
$path=$_POST['path'];//课程ID
$this->load->model('export_model');
$file = file_get_contents($path);
$xml = $this->xml_to_array($file);
//得到对应的数组
$course=$xml['course'];
unset($course['id']);
$directory=$xml['directory'];
$directory_new=array();
$fodder=$xml['fodder'];
$fodder_new=array();
$test=$xml['test'];
$test_new=array();
foreach($directory as $item)
{
unset($item['id']);
$directory_new[]=$item;
}
foreach($fodder as $item)
{
unset($item['id']);
$fodder_new[]=$item;
}
foreach($test as $item)
{
if(count($item)>1){
unset($item['id']);
$test_new[]=$item;
}
}
var_dump($course);
var_dump($directory_new);
var_dump($fodder_new);
var_dump($test_new);
}
//下面的两方法,网上找的,不记的链接了,不会告我吧
function xml_to_array($xml)
{
$array = (array)(simplexml_load_string($xml));
foreach ($array as $key=>$item){
$array[$key] = $this->struct_to_array((array)$item);
}
return $array;
}
function struct_to_array($item) {
if(!is_string($item)) {
$item = (array)$item;
foreach ($item as $key=>$val){
$item[$key] = $this->struct_to_array($val);
//下面的判断,是我自己添加,作用是,当ITEM为空时,转成空字符串,而不是空数组
if(count( $item[$key])<1)
{
$item[$key]='';
}
}
}
return $item;
}
3.数组的一些用法