如何在转换为CSV到XML后直接从我的计算机或下载文件夹中自动下载XML格式文件 [英] How to download automatically an XML format file direct from my computer or in download folder after converted into CSV to XML

查看:79
本文介绍了如何在转换为CSV到XML后直接从我的计算机或下载文件夹中自动下载XML格式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
?>

推荐答案

rows = array_map('str_getcsv',file('data.csv '));
rows = array_map('str_getcsv', file('data.csv'));


header = array_shift(
header = array_shift(


rows);


这篇关于如何在转换为CSV到XML后直接从我的计算机或下载文件夹中自动下载XML格式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆