我導出到XML的CSV文件的字符集是ASCII,所以它應該工作。獲取無效字符DOMException錯誤。我實現了htmlentities來改變特殊字符,但沒有運氣
我覺得該功能可能無法正確執行?沒有錯誤被拋出,但原始錯誤保持不變。
下面是完整的錯誤,然後代碼:
致命錯誤:未捕獲的異常「拋出:DOMException」在/home/paul/public_html/csv2xml.php:30堆棧跟蹤信息「無效字符錯誤」:#0 /home/paul/public_html/csv2xml.php(30):DOMDocument-> createElement('Listdate(YYYY -...')#1 {main}引發30行中的/home/paul/public_html/csv2xml.php
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('auto_detect_line_endings', true);
$inputFilename = 'input.csv';
$outputFilename = 'output.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(htmlentities($header));
$child = $container->appendChild($child);
$value = $doc->createTextNode($row[$i]);
$value = $child->appendChild($value);
}
$root->appendChild($container);
}
echo $doc->saveXML();
?>
因爲在元素名稱中找到的「(」字符或許? – mobius
你可以請給出導致錯誤的字符串。它們在堆棧跟蹤中不完整。但是,大括號不允許作爲元素名稱,所以這是一個原因。 – Gordon
這裏是來自CSV頂端的: 「標題」,「列表日期(YYYY-MM-DD)」,「價格」,「價格替代」,「上市價格」,「類型」,「面積」地址「,」地址2「,」城市「,」州*「 *如果我修改(和)的,會導致同樣的錯誤。 不應該轉換('s和-s和*'s? –