【问题标题】:Saving XML created with DOMDocument gives the error "DOMDocument::save() string is not in UTF-8"保存使用 DOMDocument 创建的 XML 会出现错误“DOMDocument::save() string is not in UTF-8”
【发布时间】:2011-08-26 05:05:09
【问题描述】:

我正在尝试从数据库内容生成 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

当我注释掉该行时,代码可以正常工作,但详细信息节点为空。当该行未注释时,将引发警告并且详细节点仍为空。请指教。如有必要,我可以提供更多详细信息。

【问题讨论】:

  • 你确定,$row['Detail'] 中的字符串是 UTF-8 编码的吗?
  • 当您通过utf8_encode 运行描述文本时会发生什么?
  • @Damien:文本来自mysql表,列有默认排序规则,类似于latin1 swedish ci。

标签: php xml utf-8 domdocument


【解决方案1】:

如果文本来自数据库,则该列可能不是 UTF-8,请尝试iconv

【讨论】:

  • 我尝试了 utf8_encode,它有效,还尝试了 iconv,它在本地工作,但在实时服务器中出现了一些问题。顺便说一句,utf8_encode($foo)iconv('ISO-8859-1', 'UTF-8', $foo) 之间有区别吗?
  • 如果您可以将所有内容存储在 UTF-8 中,那就最好不过了。数据库,脚本,一切。如果你能做到这一点,就不需要 iconv/utf8_encode。顺便说一句,我不确定这些功能之间有什么区别。
【解决方案2】:

我想看看description 字段的内容包裹在&lt;!CDATA[]]&gt; 中,以防万一。您可以尝试以下操作,而不是 createTextNode

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

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 2019-02-12
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    相关资源
    最近更新 更多