簡體   English   中英

獲取無效字符 DOMException 錯誤。 我已經實現了 htmlentities 來更改特殊字符,但沒有運氣

[英]Getting an invalid character DOMException error. I've implemented htmlentities to change special characters but no luck

我導出到 XML 的 CSV 文件的字符集是 ASCII,所以它應該可以工作。

我認為該功能可能沒有正確實現? 不會拋出任何錯誤,但原始錯誤保持不變。

這是完整的錯誤后跟代碼:

致命錯誤:未捕獲的異常 'DOMException' 在 /home/paul/public_html/csv2xml.php:30 中帶有消息 'Invalid Character Error' 堆棧跟蹤:#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();
?>

根據運行 PHP 5.4+,嘗試 htmlentities 中的 ENT_XML1 標志:

$child = $doc->createElement(htmlentities($header, ENT_XML1));

如果您堅持使用舊版本的 PHP,我建議您使用 htmlentitiesphp.net 注釋中的任何 XML 實體編碼函數

DOMDocument->createElement()的無效元素名稱引起 - 請參閱此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM