1
我想將我的CSV文件轉換爲XML。我正在使用以下我從帖子中獲得的腳本。但這不適合我。任何想法,爲什麼我得到這個錯誤?如何使用PHP腳本將CSV文件轉換爲XML?
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('auto_detect_line_endings', true);
$inputFilename = 'test.csv';
$outputFilename = 'test.xml';
// Open csv to read
$inputFile = fopen($inputFilename, 'rt');
// Get the headers of the file
$headers = fgetcsv($inputFile);
// Create a new dom document with pretty formatting
$doc = new DomDocument();
$doc->formatOutput = true;
// Add a root node to the document
$root = $doc->createElement('rows');
$root = $doc->appendChild($root);
// Loop through each row creating a <row> node with the correct data
while (($row = fgetcsv($inputFile)) !== FALSE)
{
$container = $doc->createElement('row');
foreach($headers as $i => $header)
{
$child = $doc->createElement($header);
$child = $container->appendChild($child);
$value = $doc->createTextNode($row[$i]);
$value = $child->appendChild($value);
}
$root->appendChild($container);
}
$strxml = $doc->saveXML();
我得到的錯誤是在這裏:
Uncaught exception 'DOMException' with message 'Invalid Character Error'
這裏是我的csv文件:
name, email, test
john,[email protected],blah
mary,[email protected],something
jane,[email protected],blarg
bob,[email protected],asdfsfd
https://stackoverflow.com/questions/4852796/php-script-to-convert-csv-files-to-xml – MikeyBunny
@MikeyBunny是的,我在那篇文章之後。但這不適合我。 – Cristal
你可以包括你的CSV文件,即使前幾行會有所幫助。 –