8

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

しかし、これは一般的な印刷形式を示しています。XML タイプのページとして表示するにはどうすればよいですか?

4

4 に答える 4

18

ファイルを直接出力する場合は、XML ヘッダーを設定する必要があります。

Codeigniter出力クラスの使用:

$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); 
于 2012-04-28T06:19:39.750 に答える
2

私も同じ質問をしました。私はそれをググった。この解決策を見つけました。そして、それは私にとって完璧に機能します。 ここをクリックしてソースコードを入手

ダウンロードして解凍するだけです(解凍してください)

次に、解凍​​したフォルダーの application->libraries にある xml_writer.php を Codeigniter プロジェクトの libraries フォルダーにコピーます。

また、application->controllerのxml.phpを controllers フォルダーにコピーします。

最後に、抽出したフォルダーのビューにあるxml.phpをビューにコピーして実行します。

それでおしまい...

于 2013-08-25T13:25:11.220 に答える
0

CodeIgniter 4 の場合は、はるかに簡単です。統合された応答オブジェクトを使用して、次のようなことを行うことができます:

return $this->response->setXML($xmlString);

これにより、非常に単純化されます。私の場合、ビューを使用して XML を生成し、同じことを使用して XML を出力するだけです。

return $this->response->setXML(view('myfeed'));
于 2021-06-21T16:40:35.000 に答える