Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用SimpleXML创建具有命名空间的RSS元素_Php_Xml_Rss - Fatal编程技术网

在PHP中使用SimpleXML创建具有命名空间的RSS元素

在PHP中使用SimpleXML创建具有命名空间的RSS元素,php,xml,rss,Php,Xml,Rss,我正在尝试创建一个rss提要,其中一个元素是 <content:encoded></content:encoded> 我得到的结果是: <encoded> .................. </encoded> 。。。。。。。。。。。。。。。。。。 我没有得到内容命名空间,我如何才能得到它?正如您在中看到的,您需要提供命名空间URI作为addChild()的第三个参数,以便在命名空间中正确创建元素: $item->addChild(

我正在尝试创建一个rss提要,其中一个元素是

<content:encoded></content:encoded>
我得到的结果是:

<encoded> .................. </encoded>
。。。。。。。。。。。。。。。。。。
我没有得到内容命名空间,我如何才能得到它?

正如您在中看到的,您需要提供命名空间URI作为
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>
<?xml version="1.0"?>
<root xmlns:content="mynamespace"><content:encoded>foo bar baz</content:encoded></root>