XML creation using CodeIgniter
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);
I want to create xml data with attributes in codeigniter
Here we need to use of createAttribute.
public function display_xml()
{
$this->load->dbutil();
$data = $this->db->query('select * from markers');
$markers_data = $data->result_array();
$this->output->set_content_type('text/xml');
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("markers");
$dom->appendChild($root);
foreach ($markers_data as $value)
{
// create child element
$marker = $dom->createElement("marker");
$root->appendChild($marker);
// create attribute node
$id = $dom->createAttribute("id");
$marker->appendChild($id);
// create attribute value node
$priceValue = $dom->createTextNode($value['id']);
$id->appendChild($priceValue);
// create attribute node
$name = $dom->createAttribute("name");
$marker->appendChild($name);
// create attribute value node
$nameValue = $dom->createTextNode($value['name']);
$name->appendChild($nameValue);
// create attribute node
$address = $dom->createAttribute("address");
$marker->appendChild($address);
// create attribute value node
$addressValue = $dom->createTextNode($value['address']);
$address->appendChild($addressValue);
// create attribute node
$lat = $dom->createAttribute("lat");
$marker->appendChild($lat);
// create attribute value node
$latValue = $dom->createTextNode($value['lat']);
$lat->appendChild($latValue);
// create attribute node
$lng = $dom->createAttribute("lng");
$marker->appendChild($lng);
// create attribute value node
$lngValue = $dom->createTextNode($value['lng']);
$lng->appendChild($lngValue);
// create attribute node
$type = $dom->createAttribute("type");
$marker->appendChild($type);
// create attribute value node
$typeValue = $dom->createTextNode($value['type']);
$type->appendChild($typeValue);
}
// save and display tree
echo $dom->saveXML();
}
placing data in xml file from database in codeigniter?
Generating xml file from database is easy. You dont need loop just follow this.
Model Function
function getDataForXML(){
return $query = $this->db->get('my_table');
//Here you should note i am returning
//the query object instead of
//$query->result() or $query->result_array()
}
Controller
function get_report()
{
$this->load->model('my_model');
$this->load->dbutil();
$this->load->helper('file');
// get the object
$report = $this->my_model->getDataForXML();
//pass it to db utility function
$new_report = $this->dbutil->xml_from_result($report);
//Now use it to write file. write_file helper function will do it
write_file('xml_file.xml',$new_report);
//Done
}
And a new file with the name xml_file.xml is available now!
Resources
Database Utility Class
And
File Helper
How to write xml file to server in php framework codeigniter?
your xml is looking good for write use the following code
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/export.xml", $xml);
with $_SERVER['DOCUMENT_ROOT']
you will get the root directory URL.
Generate XML file from database - Codeigniter
Take a look into the Databse utils class, it has a xml_from_result
method that will try to generate xml from any database result objects.
How to create dynamic sitemap in Codeigniter
You are missing header
setting in your controller, put the header like this
Class Sitemap extends CI_Controller {
function sitemap()
{
$query= $this->db->query("SELECT url_slug FROM snippets UNION SELECT tag_name FROM tags");
$data = $query->result();
$data['items'] =$data;
header("Content-Type: text/xml;charset=iso-8859-1");
$this->load->view('sitemap', $data);
}
}
Your view file seems to be ok.
For more details Refer this link
Related Topics
Change Name of Laravel's Created_At and Updated_At
Php: How to Generate a Hmacsha256 Signature of a String
PHP Session Ids -- How Are They Generated
How to Set Current Page "Active" in PHP
Regex/Domdocument - Match and Replace Text Not in a Link
How to Set a Maximum Size Limit to PHP Curl Downloads
Select Last 20 Order by Ascending - PHP/Mysql
Where to Put the PHP Artisan Migrate Command
Send Checkbox Value in PHP Form
Codeigniter Csrf Valid for Only One Time Ajax Request
How to Install an Older Version of PHPunit Through Pear
Removing Black Borders 4:3 on Youtube Thumbnails
How to Send Cookies with File_Get_Contents
Get the Price of an Item on Steam Community Market with PHP and Regex
PHP - Time Remaining Until Specific Time from Current Time of Page Load
Why Does PHP Not Complain When I Treat a Null Value as an Array Like This
Swift_Transportexception Connection Could Not Be Established with Host Smtp.Gmail.Com