【发布时间】:2012-05-04 08:54:25
【问题描述】:
我只是想将 cdata 添加到 xml 节点 - 描述。我的xml函数如下。我尝试在我的函数中对php.net 使用以下函数的位
<?php
function updateXMLFile($itemName, $description, $pageName, $imageFileName)
{
$imageSrc = "<img src='http://nicolaelvin.com/authoring/phpThumb/phpThumb.php?src=../images/" . $imageFileName . "&w=100'/>";
$id = strtolower($id = str_replace(' ', '_', $itemName));
$directLinkToItem = 'http://nicolaelvin.com/authoring/' . $pageName . '.php#' . $id;
$xml = simplexml_load_file('nicolaElvinsPortfolio.xml');
$item = $xml->channel->addChild('item');
$item->addChild('title', $itemName);
$item->addChild('pubDate', date('r'));
$item->addChild('link', $directLinkToItem);
$item->addChild('description');
$cdata->description->createCDATASection('testyfhgjhsgsdjahgs');
$item->appendChild($cdata);
///Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
//Save XML to file - remove this and following line if save not desired
$dom->save('nicolaElvinsPortfolio.xml');
}
//function from php.net
function sxml_cdata($path, $string)
{
$dom = dom_import_simplexml($path);
$cdata = $dom->ownerDocument->createCDATASection($string);
$dom->appendChild($cdata);
}
?>
【问题讨论】:
-
我已经尝试了其中说创建 CDATASection 的位,它是我从 php.net 的函数改编而来的。我没有收到任何错误,只是没有向我的 XML 文档添加任何内容。
-
我不确定 simpleXML 是否支持该功能。顾名思义,它用于对 XML 的简单操作。您可能不得不求助于完整的 DOM 类。
-
哦,好的,谢谢,我不知道该怎么做,所以我可能不得不把它排除在外
-
您确定没有收到任何错误吗?具体来说,您确定您没有按照
Attempt to call method 'createCDATASection()' on a non-object的方式获得 E_FATAL 吗?我问的原因是你永远不会在updateXMLFile()函数的范围内定义$cdata...