我使用这个代码笨生成XML:
public function get_cuisine()
{
$this->load->dbutil();
$sql = "select * from cuisine";
$query = $this->db->query($sql);
$config = array (
'root' => 'root',
'element' => 'element',
'newline' => "\n",
'tab' => "\t"
);
echo $this->dbutil->xml_from_result($query, $config);
}
但是,这显示了普通打印格式。 我怎样才能得到它显示为一个XML类型的页面?
你需要,如果要直接输出的文件设置XML标题:
使用笨输出类 :
$xml = $this->dbutil->xml_from_result($query, $config);
$this->output->set_content_type('text/xml');
$this->output->set_output($xml);
或者你也可以使用纯PHP来设置标题 :
header('Content-type: text/xml');
echo $this->dbutil->xml_from_result($query, $config);
或者你可以使用CI 下载助手 :
$xml = $this->dbutil->xml_from_result($query, $config);
$this->load->helper('download');
force_download('myfile.xml', $xml);
或者把它写入与文件文件帮助 :
$xml = $this->dbutil->xml_from_result($query, $config);
$this->load->helper('file');
$file_name = '/path/to/myfile.xml';
write_file($file_name, $xml);
// Optionally redirect to the file you (hopefully) just created
redirect($file_name);
我也有同样的问题。 我用Google搜索它。 发现了这个解决方案。 和它的作品完美的我。 点击这里获取源代码
只需下载并解压缩(解压缩)
然后xml_writer.php复制应用程序- >提取的文件夹到您的库库在你的笨项目文件夹。
在应用程序- >控制器xml.php也复制到你的控制器文件夹
最后,在提取的文件夹的视图的xml.php复制到您的视图,并运行它..
而已...
定制的解决方案:
$mysql_data = $this->db->get('products')
->result_array();
$xml = '<root>';
foreach($mysql_data as $row){
$xml .= '<item>
<name>'.$row['title'].'</name>
<price>'.$row['price'].'</price>
<image>'.$row['pic'].'</image>
</item>';
}
$xml .= '</root>';
$this->output->set_content_type('text/xml');
$this->output->set_output($xml);