将rss xmlns名称空间定义添加到php simplexml文档中? [英] Add rss xmlns namespace definition to a php simplexml document?

查看:74
本文介绍了将rss xmlns名称空间定义添加到php simplexml文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php5的simplexml创建一个iTunes有效的播客feed:

I'm trying to create an itunes-valid podcast feed using php5's simplexml:

<?php   
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>


<channel>
</channel>
XML;

$xml_generator = new SimpleXMLElement($xml_string);
           $tnsoundfile = $xml_generator->addChild('title', 'Main Title');
           $tnsoundfile->addChild('itunes:author', "Author", ' ');
           $tnsoundfile->addChild('category', 'Audio Podcasts'); 
           $tnsoundfile = $xml_generator->addChild('item');
           $tnsoundfile->addChild('title', 'The track title');        
           $enclosure = $tnsoundfile->addChild('enclosure');
           $enclosure->addAttribute('url', 'http://test.com');
           $enclosure->addAttribute('length', 'filelength');
           $enclosure->addAttribute('type', 'audio/mpeg');       
           $tnsoundfile->addChild('itunes:author', "Author", ' '); 


header("Content-Type: text/xml");
echo $xml_generator->asXML();

?>

它没有验证,因为我必须把这一行放在上面:

It doesn't validate, because I've got to put the line:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

按照 http://www.apple.com/itunes/podcasts/specs .html .

因此输出应为:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>

等我已经遍及手册和论坛了,只是做得不好.如果我放的话,在页脚附近:

etc. I've been over and over the manual and forums, just can't get it right. If I put, near the footer:

header("Content-Type: text/xml");
echo '<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">';
echo $xml_generator->asXML();
?>

然后它在firefox中看起来很正确,并且不再抱怨未定义的名称空间,但是feedvalidator抱怨

Then it sort of looks right in firefox and it doesn't complain about undefined namespaces anymore, but feedvalidator complains that

第1行,第77列:XML解析错误: :1:77:xml声明不在 外部实体的开始[帮助]

line 1, column 77: XML parsing error: :1:77: xml declaration not at start of external entity [help]

因为该文档现在开始:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><?xml version="1.0" encoding="UTF-8"?>

而不是

<?xml version="1.0" encoding="UTF-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

推荐答案

使用SimpleXML很有可能.只需在构造函数字符串中声明名称空间,而不是将其声明为属性.

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');

这篇关于将rss xmlns名称空间定义添加到php simplexml文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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