Add rss xmlns namespace definition to a php simplexml document?

蓝咒 提交于 2019-12-06 05:57:30

This is very much possible with SimpleXML. Just declare the namespace within the constructor string, not as an attribute.

$rss_xml = new SimpleXMLElement(
   '<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"/>');
$rss_xml->addAttribute('version', '2.0');

The code in shown in the question doesn't work because it doesn't use the correct namespace. Specifically, these lines:

$tnsoundfile->addChild('itunes:author', "Author", ' ');

They will create an <author/> node in the " " (one space) namespace, which is obviously incorrect. It should read:

$tnsoundfile->addChild('itunes:author', "Author", 'http://www.itunes.com/dtds/podcast-1.0.dtd');

This is the correct way to use namespaces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!