使用CodeIgniter创建XML [英] XML creation using CodeIgniter

查看:101
本文介绍了使用CodeIgniter创建XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Codeigniter中使用这个代码来生成XML:

I'm using this code in Codeigniter to generate 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);   
}   

但这显示了一般的打印格式。

But this shows the general print format. How can I get it to show as an XML type page?

推荐答案

如果您想要设置XML标头,直接输出文件:

You'll need to set XML headers if you want to output the file directly:

使用Codeigniter 输出类

Using the Codeigniter Output class:

$xml = $this->dbutil->xml_from_result($query, $config);
$this->output->set_content_type('text/xml');
$this->output->set_output($xml); 

也可以使用纯PHP设置标头

Or you can use plain PHP to set the headers:

header('Content-type: text/xml');
echo $this->dbutil->xml_from_result($query, $config);

或者您可以使用CI 下载助手

Or you can use the CI download helper:

$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); 

这篇关于使用CodeIgniter创建XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆