Create XML file from mysql table


Hello Experts today i will discus about a XML write function and this XML value come from mysql table. Its very simple for use. Here is a sample mysql table and steps are described. I think it will be helpful for Experts…

<?php
/**
* writeXML : Converting mysql table into XML
*
* @author : S.M. Saidur Rahman ,
* Moderator, joomla_experts (http://tech.groups.yahoo.com/group/joomla_experts/)
Moderator, cakephpexperts (http://tech.groups.yahoo.com/group/cakephpexperts/)
* URL: https://ranawd.wordpress.com/
* @version : 1.0
* @date 2008-07-09
* Purpose : Write XML file and collect data from mysql table
*/

/*
@Steps:
#Create a table name “sampletable”
#Create a XML file name “sample.xml”
#Correct root path as define value
#Call this function and Enjoy!
*/

//Here is an example of mysql table
CREATE TABLE `sampletable` (
`id` int(11) NOT NULL auto_increment,
`image` varchar(255) collate latin1_general_ci NOT NULL,
`thumbnail` varchar(255) collate latin1_general_ci NOT NULL,
`caption` varchar(150) collate latin1_general_ci NOT NULL,
`ordering` int(11) NOT NULL default ‘0’,
PRIMARY KEY (`id`)
)

//Define XML file root path
define(‘ROOT_PATH’, $_SERVER[‘DOCUMENT_ROOT’]);

//XML write function
function writeXML($xmlfile, $tablename)
{
$xmlfile_path= ROOT_PATH.”/”.$xmlfile;
$query_xmlw = “SELECT * FROM $tablename ORDER BY ordering DESC”;
$result_xmlw = mysql_query($query_xmlw);

$xmlstore=”;
$xmlstore .=”;

while($row_xmlw = mysql_fetch_array($result_xmlw)) {
$xmlstore .=”.$row_xmlw[‘image’].”;
$xmlstore .=”.$row_xmlw[‘thumbnail’].”;
$xmlstore .=’

‘.$row_xmlw[‘caption’].’

‘;
$xmlstore .=”;
}

$xmlstore .=”;

$handle = fopen($xmlfile_path, ‘w’);

fwrite($handle, $xmlstore);
}

//Put XML file name and mysql table name simultaniously
writeXML(‘sample.xml’, ‘sampletable’);
?>