简体   繁体   English

使用CodeIgniter创建XML

[英]XML creation using CodeIgniter

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

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: 如果要直接输出文件,则需要设置XML标头:

Using the Codeigniter Output class : 使用Codeigniter 输出类

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

Or you can use plain PHP to set the headers : 或者您可以使用纯PHP来设置标头

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

Or you can use the CI download helper : 或者您可以使用CI 下载帮助程序

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

Or write it to a file with the file helper : 或者使用文件助手将其写入文件

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

I had also the same question. 我也有同样的问题。 I have googled it. 我用谷歌搜索了它。 Found this solution. 找到了解决方案。 And it works perfectly for me. 它对我来说非常适合。 Click here to get the source code 单击此处获取源代码

Just download and unzip ( extract it) 只需下载并解压缩(解压缩)

Then copy the xml_writer.php in application->libraries of the extracted folder to your libraries folder in your Codeigniter project. 然后将压缩文件夹的application-> libraries中的xml_writer.php复制到Codeigniter项目中的libraries文件夹中。

Also copy the xml.php in the application->controller in to your controllers folder 同时将application-> controller中的xml.php复制到controllers文件夹中

Finally copy the xml.php in the views of the extracted folder in to your view and run it.. 最后将压缩文件夹视图中的xml.php复制到视图中并运行它。

That's it... 而已...

Custom solution: 定制解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM