Links & Resources

Home
Tutorial
CSV To XML
Email Scripts
Captcha
Display a Table

sponsored link

sponsored link

CSV to XML

Creating an xml file from a csv file.

The first lines prevent the page being cached on the local computer http 1.1 and http 1.0. and send a header to the browser telling it to expect an xml formatted file.

Create a global tag round the whole xml file and another tag to wrap arround each section (row) of data.

Open the csv file and get the column headings which become your metatags.

Now read each row in turn and wrap the tags round each piece of data.

Finally echo the xml to the browser.

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Content-type: text/xml');
$global_tag="product_list";
$outer_tag="product";
$tags=array();
$xml="<?xml version=\"1.0\" ?>\r\n";
$xml.="<".$global_tag.">";
$fp=fopen("filname.csv","r");
$data = fgetcsv ($fp,8192);
$tags=$data;
while ($data=fgetcsv ($fp,8192)){
$xml.="<".$outer_tag.">";
foreach ($tags as $key=>$value){
$xml.="<".$value.">";
$xml.="<![CDATA[".$data[$key]."]]>";
$xml.="</".$value.">\r\n";
}
$xml.="</".$outer_tag.">\r\n";
}
$xml.="</".$global_tag.">";
echo $xml;
?>