我尝试阅读我的iTunes RSS.我可以阅读标题,甚至是itunes:字幕,但我的标签图片有问题.
饲料:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>title of the podcast</title>
<itunes:image href="http://www.MyWeb/myImg.png"/>
</channel>
</rss>
PHP:
$xml=("http://www.myWeb/rss.xml");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')//normal tag
->item(0)->childNodes->item(0)->nodeValue;
$channel_image = $channel->getElementsByTagName('image') //problem
->item(0)->childNodes->item(0)->nodeValue;
echo $channel_title . '<br>';
echo $channel_image . '<br>';
解决方法:
你可以使用SimpleXML.因为image元素有一个名称空间前缀(itunes),所以你必须使用children方法传递名称空间URL,然后调用attributes方法:
$feed = simplexml_load_file('http://www.myWeb/rss.xml');
foreach ($feed->channel as $channel) {
$ns_itunes = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
echo $ns_itunes->image->attributes();
}