保存使用DOMDocument创建的XML会出现错误"DOMDocument :: save()字符串不在UTF-8中". [英] Saving XML created with DOMDocument gives the error "DOMDocument::save() string is not in UTF-8"

查看:44
本文介绍了保存使用DOMDocument创建的XML会出现错误"DOMDocument :: save()字符串不在UTF-8中".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库内容生成RSS.这是代码的相关片段:

I am trying to generate RSS from database content. Here is the relevant fragment of the code:

$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并引发错误:

The problem, as you see, is that that DOMDocument is not able to insert the details into the RSS and throws the error:

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

当我注释掉该行时,代码可以正常运行,但细节"节点为空.当该行取消注释时,将发出警告,并且细节节点仍为空.请指教.如有必要,我会提供其他详细信息.

When I comment out the line, the code works OK but The details node is empty. When the line is un-commented, the warning is raised and the detail node is still empty. Please advice. I'll can provide additional details if necessary.

推荐答案

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

If the text comes from database maybe the column is not in UTF-8, try iconv.

这篇关于保存使用DOMDocument创建的XML会出现错误"DOMDocument :: save()字符串不在UTF-8中".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆