Сохранение XML, созданного с помощью DOMDocument, приводит к ошибке «Строка DOMDocument :: save () не находится в UTF-8"

Я пытаюсь создать RSS из содержимого базы данных. Вот соответствующий фрагмент кода:

$doc = new DOMDocument(); $doc->formatOutput = true; $doc->preserveWhiteSpace = false; if(is_file($filePath)) { $doc->load($filePath); } else { $doc->loadXML(' <rss version="2.0"> <channel> <title></title> <description></description> <link></link> </channel></rss> '); } . . . $titleText = $row['Subject']; $descriptionText = $row['Detail']; // this row has the problem $linkText = sprintf('http://www.domain.com/%s', $row['URL']); $pubDateText = date(DATE_RSS, strtotime($row['Created'])); $titleNode = $doc->createElement('title'); $descriptionNode = $doc->createElement('description'); $linkNode = $doc->createElement('link'); $pubDateNode = $doc->createElement('pubDate'); $titleNode->appendChild($doc->createTextNode($titleText)); $descriptionNode->appendChild($doc->createTextNode($descriptionText)); $linkNode->appendChild($doc->createTextNode($linkText)); $pubDateNode->appendChild($doc->createTextNode($pubDateText)); $itemNode = $doc->createElement('item'); $itemNode->appendChild($titleNode); $itemNode->appendChild($descriptionNode); $itemNode->appendChild($linkNode); $itemNode->appendChild($pubDateNode); $channelNode = $doc->getElementsByTagName('channel')->item(0); $channelNode->appendChild($itemNode); $doc->save($filePath); // this is where warning is raised 

И вот вывод:

 <?xml version="1.0"?> <rss version="2.0"> <channel> <title>ALPHA BRAVO CHARLIE</title> <description>DELTA ECHO FOXTROT</description> <link>http://www.xxxxxxx.yyy/</link> <item> <title>Title Here</title> <description/><!-- this node has the problem --> <link>http://www.xxxxxxx.yyy/article/12345678/</link> <pubDate>Sun, 01 May 2011 23:18:28 +0500</pubDate> </item> </channel> </rss> 

Проблема, как вы видите, заключается в том, что DOMDocument не может вставить данные в RSS и выдает ошибку:

 Warning: DOMDocument::save() [domdocument.save]: string is not in UTF-8 in C:\Inetpub\wwwroot\cron-rss.php on line 66 

Когда я прокомментирую строку, код работает нормально, но узел сведений пуст. Когда строка не комментируется, предупреждение поднимается, а узел детали все еще пуст. Пожалуйста посоветуй. При необходимости я могу предоставить дополнительные данные.

Если текст поступает из базы данных, возможно, столбец не находится в UTF-8, попробуйте iconv .

В верхней части моей головы я хотел бы увидеть содержимое поля description заключенного в <!CDATA[]]> , на всякий случай. Вместо createTextNode вы можете попробовать следующее:

 $descriptionNode->appendChild($doc->createCDATASection($descriptionText));