Creating XML Using CodeIgniter

I use 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 it shows the general print format. How can I show it as an XML type page?

+7
source share
3 answers

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

Using 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 simple PHP to set the headers :

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

Or you can use the CI helper to download :

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

Or write it to a file using the file assistant :

 $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); 
+15
source

I had the same question. I ruined him. Found this solution. And it works great for me. Click here to get the source code.

Just download and unzip (extract it)

Then copy xml_writer.php to the application-> library of the extracted folder into your libraries folder in the Codeigniter project.

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

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

What is it...

+2
source

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

All Articles