我正在尝试创建一个RSS供稿,其中一个元素是
<content:encoded></content:encoded>
但是,当我使用此代码时:
$item->addChild('content:encoded',htmlspecialchars($itemdata->description));
我得到的结果是:
<encoded> .................. </encoded>
我没有内容名称空间,怎么办?
正如您在文档中看到的那样,您需要提供名称空间URI作为的第3个参数,addChild()
以便在名称空间中正确创建元素:
$item->addChild(
'content:encoded',
htmlspecialchars($itemdata->description),
'namespace-URI-for-content-prefix-here'
);
快速演示:
$raw = '<root xmlns:content="mynamespace"></root>';
$item = new SimpleXMLElement($raw);
$item->addChild(
'content:encoded',
'foo bar baz',
'mynamespace'
);
echo $item->asXML();
输出 :
<?xml version="1.0"?>
<root xmlns:content="mynamespace"><content:encoded>foo bar baz</content:encoded></root>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句