我在从我的itunes XML Feed中获取信息时遇到了一些麻烦,你可以在这里查看:http://c3carlingford.org.au/podcast/C3CiTunesFeed.xml
我需要从每个内部<item>
标记中获取信息。其中一个例子如下:
<item>
<title>What to do when a viper bites you</title>
<itunes:subtitle/>
<itunes:summary/>
<!-- 4000 Characters Max ******** -->
<itunes:author>Ps. Phil Buechler</itunes:author>
<itunes:image href="http://www.c3carlingford.org.au/podcast/itunes_cover_art.jpg"/>
<enclosure url="http://www.ccccarlingford.org.au/podcast/C3C-20120722PM.mp3" length="14158931" type="audio/mpeg"/>
<guid isPermaLink="false">61bc701c-b374-40ea-bc36-6c1cdaae8042</guid>
<pubDate>Sun, 22 Jul 2012 19:30:00 +1100</pubDate>
<itunes:duration>40:01</itunes:duration>
<itunes:keywords>
Worship, Reach, Build, Holy Spirit, Worship, C3 Carlingford
</itunes:keywords>
</item>
现在我取得了一些成功! 我已经能够获得所有的标题:
<?php
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->load('http://c3carlingford.org.au/podcast/C3CiTunesFeed.xml');
$items = $dom->getElementsByTagName('item');
foreach($items as $item){
$title = $item->getElementsByTagName('title')->item(0)->nodeValue;
echo $title . '<br />';
};
?>
但我似乎无法得到任何其他东西......我对这一切都是新手!
所以我需要了解的内容包括:
<itunes:author>
值。<enclosure>
代码有人会帮助我取出这两个价值吗?
答案 0 :(得分:5)
您可以使用DOMXPath
执行此操作,让您的生活更轻松:
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML( $xml); // $xml = file_get_contents( "http://www.c3carlingford.org.au/podcast/C3CiTunesFeed.xml")
// Initialize XPath
$xpath = new DOMXpath( $doc);
// Register the itunes namespace
$xpath->registerNamespace( 'itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
$items = $doc->getElementsByTagName('item');
foreach( $items as $item) {
$title = $xpath->query( 'title', $item)->item(0)->nodeValue;
$author = $xpath->query( 'itunes:author', $item)->item(0)->nodeValue;
$enclosure = $xpath->query( 'enclosure', $item)->item(0);
$url = $enclosure->attributes->getNamedItem('url')->value;
echo "$title - $author - $url\n";
}
您可以从the demo看到这将输出:
What to do when a viper bites you - Ps. Phil Buechler - http://www.ccccarlingford.org.au/podcast/C3C-20120722PM.mp3
答案 1 :(得分:2)
是的,您可以使用simplexml。
以下是示例代码:
<?php
$x = simplexml_load_file("http://c3carlingford.org.au/podcast/C3CiTunesFeed.xml");
foreach ($x->channel->item as $item) {
$otherNode = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
echo $item->title .'---'.$otherNode->author;
echo "\n";
}
?>
<强>输出:强>
当毒蛇咬你时该怎么办--- Ps。 Phil Buechler
活水,让河水流动!--- Ps。 Phil Buechler
上帝呼召彼此原谅AM&amp; PM ---诗篇。理查德博塔
上帝呼召福音传播AM&amp; PM --- Rob Waugh
上帝呼召彼此相爱AM&amp; PM --- Rob Waugh
希望这有帮助!
答案 2 :(得分:0)
您可以使用simpleXML个孩子
$ item-> children('itunes',TRUE);
因此,您拥有一个数组,其中包含所有标签itunes:duration,itunes:subtitle...。
<?php
$x = simplexml_load_file("http://c3carlingford.org.au/podcast/C3CiTunesFeed.xml");
foreach ($x->channel->item as $item) {
$otherNode = $item->children('itunes', TRUE);
echo $otherNode->duration;
echo "\n";
echo $otherNode->author;
echo "\n";
echo $otherNode->subtitle;
echo "\n";
}
?>