8

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?

4 Answers 4

18

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

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

Or you can use plain PHP to set the headers:

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

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

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please little elaborate about the $file_name = '/path/to/myfile.xml'; my file path is $file_name = base_url('application/controllers/test.xml'); I guess the problem is into $file_name.
@Shiplu use the full server path to the file, not your website's URL.
2

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.

Also copy the xml.php in the application->controller in to your controllers folder

Finally copy the xml.php in the views of the extracted folder in to your view and run it..

That's it...

Comments

0

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

Comments

0

For CodeIgniter 4 it's much easier. You can use the integrated response object and just do something like:

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

This simplifies it a lot, in my case I generate the XML using views and then just output the XML using this same thing, just something like:

return $this->response->setXML(view('myfeed'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.