使用CodeIgniter创建XML

Sou*_*aha 8 php xml codeigniter

我在Codeigniter中使用此代码生成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);   
}   
Run Code Online (Sandbox Code Playgroud)

但这显示了一般的打印格式.如何将其显示为XML类型页面?

Wes*_*rch 17

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

使用Codeigniter 输出类:

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

或者您可以使用纯PHP来设置标头:

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

或者您可以使用CI 下载帮助程序:

$xml = $this->dbutil->xml_from_result($query, $config);
$this->load->helper('download');
force_download('myfile.xml', $xml);
Run Code Online (Sandbox Code Playgroud)

或者使用文件助手将其写入文件:

$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); 
Run Code Online (Sandbox Code Playgroud)