How to create XMLFile Form Mysql Table
function createXMLFile($result, $feildLength, $dir) {
$headers = array();
$xmlData = array();
$str = "";
$counter = 0;
for ($i=0;$i<$feildLength;$i++) {
array_push($headers, mysql_field_name($result, $i));
}
$xmlFileData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
while ($row = mysql_fetch_array($result)) {
for ($i=0;$i<$feildLength;$i++) {
$str .= "<".trim($headers[$i]).">" . escapeXMLEntities(trim($row[$i])) . "</".trim($headers[$i]).">\n";
$counter++; //increment column#
if ($counter == $feildLength) { //new row
array_push($xmlData, "<record>\n" . $str . "</record>\n");
$str = "";
$counter = 0;
}
}
}
$xmlFileData .= "<records name=\"Customer_Table\">\n" . implode("", $xmlData) . "</records>";
$f = fopen($dir."xmlDataFile.xml", 'w+') or die("Can not create file: xmlDataFile.xml");
fwrite($f, $xmlFileData);
fclose($f);
}
function escapeXMLEntities($xml) { // this will escape xml entities
$xml = str_replace("&", "&", $xml);
$xml = str_replace("<", "<", $xml);
$xml = str_replace(">", ">", $xml);
$xml = str_replace("\"", """, $xml);
$xml = str_replace("'", "'", $xml);
return $xml;
}
# Call this function
createXMLFile(mysql_query('select * from Customer'), 10, "/Folder/");
Comments
Post a Comment