使用PHP DOM方法读取itunes XML文件


Reading itunes XML file with PHP DOM method

我在从itunes XML提要中获取信息时遇到了一些问题,您可以在此处查看: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 />';
    };
?>

但我似乎什么都拿不出来。。。我是这一切的新手!


所以我需要得到的包括:

  1. <itunes:author>
  2. <enclosure>标记的url属性值

有人能帮我把这两个价值观拿出来吗?

您可以使用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";
}

你可以从演示中看到,这将输出:

What to do when a viper bites you - Ps. Phil Buechler - http://www.ccccarlingford.org.au/podcast/C3C-20120722PM.mp3

是的,您可以使用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";
 } 
?> 

输出:

毒蛇咬你该怎么办。Phil Buechler

活水,让河水流过---Phil Buechler

上帝的呼召彼此宽恕AM&下午---下午。Richard Botta

上帝对福音传道者AM&PM---Rob Waugh

上帝的呼唤爱彼此AM&PM---Rob Waugh

希望得到帮助!

您可以使用simpleXML子级

$item->children('tunes',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";
  } 
?>