Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need help sir, I want to automatically download an XML format files from my computer or download folder.. Now its only saving on my working directory the xml converted file, Is that possible Automatically Download save after converted into XML?


What I have tried:

<pre><?php
// Map CSV file to array
$rows = array_map('str_getcsv', file('data.csv'));
$header = array_shift($rows);
$data = array();
foreach ($rows as $row)
{
    $data[] = array_combine($header, $row);
}
// Process Data if need be
foreach($data AS $key => $val)
{
    // Processing here
}
 //Creates XML string and XML document using the DOM 
$xml = new DomDocument('1.0', 'UTF-8'); 
//Add root node
$root = $xml->createElement('root');
$xml->appendChild($root);
// Add child nodes
foreach($data AS $key => $val) 
{   
    $entry = $xml->createElement('entry');
    $root->appendChild($entry);

    foreach($val AS $field_name => $field_value) 
    {   
        $field_name = preg_replace("/[^A-Za-z0-9]/", '', $field_name); // preg_replace has the allowed characters
        $name = $entry->appendChild($xml->createElement($field_name)); 
        $name->appendChild($xml->createCDATASection($field_value)); 
    }
}
// Set the formatOutput attribute of xml to true
$xml->formatOutput = true; 
// Output to screen
//header('Content-Type: text/xml');
//echo $xml->saveXML();
// Save as file
$xml->save('xml-import.xml'); // save as file
?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900