Write The XML File


Recently I have made a function that write xml file and bring data from mysql database. Its very easy to use. First put this function and call that function after mysql row insert, update or delete. Finally we may use this xml for different purpose. I think it will be helpful for someone.

function xmlfilewrite($xmlfile, $tablename)
{
// Write The XML File
$xmlfile_path= ROOT_PATH.”/”.$xmlfile; // ROOT_PATH like define(‘ROOT_PATH’, $_SERVER[‘DOCUMENT_ROOT’].”/sitename”);
$query_xmlw = “SELECT * FROM $tablename ORDER BY ordering DESC”;
$result_xmlw = mysql_query($query_xmlw);

$xmlstore='<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>’;
$xmlstore .='<images>’;

while($row_xmlw = mysql_fetch_array($result_xmlw)) {
$xmlstore .='<pic><image>’.$row_xmlw[‘image’].'</image>’;
$xmlstore .='<thumbnail>’.$row_xmlw[‘thumbnail’].'</thumbnail>’;
$xmlstore .='<caption>’.$row_xmlw[‘caption’].'</caption>’; //Create XML with caption
$xmlstore .='</pic>’;
}

$xmlstore .='</images>’;
$handle = fopen($xmlfile_path, ‘w’);
fwrite($handle, $xmlstore);
}

$this->xmlfilewrite(‘xmlfile.xml’, ‘tablename’); // write the xml file
?>

Here the MySql table i have used
CREATE TABLE `tablename` (
`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`)
) ;

And finally create this type XML
<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>
<images>
<pic>
<image>1211873814.jpg</image>
<thumbnail>thumb_1211873814.jpg</thumbnail>
<caption>Test Image</caption>
</pic>
</images>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s