简体   繁体   English

转换为CSV TO XML后,如何直接从我的计算机或下载文件夹中自动下载xml格式的文件

[英]How to Download Automatically an xml format file direct from my computer or in download folder after converted into CSV TO 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? 先生,我需要帮助,我想从我的计算机或下载文件夹中自动下载XML格式的文件。.现在,它仅将xml转换后的文件保存在我的工作目录中,请问转换为XML后是否可以自动下载保存?

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

DomDocument has a saveXML method which returns a string that you can pass to the browser. DomDocument有一个saveXML方法,该方法返回一个可以传递给浏览器的字符串。 http://php.net/manual/de/domdocument.savexml.php http://php.net/manual/de/domdocument.savexml.php

$str_xml = $xml->saveXML();
echo $str_xml;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM