php - 使用 PHP DOM 方法读取 iTunes XML 文件

我在从我的 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>值(value)。
  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";
}

从the demo可以看出这将输出:

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

https://stackoverflow.com/questions/11612712/

相关文章:

c# - C#中如何从子构造函数调用父构造函数

php - 如何在php中显示有限的单词

sql - UNION ALL 之后的 CTE

php - 用于匹配标点符号和字母数字字符的正则表达式

ruby-on-rails - RAILS3 : to_JSON with multiple obj

jruby - bundler 无法识别平台?找不到 gem

r - 根据焦点画一个椭圆

bash - 获取 n 个最后记录并更改它们的特定列

performance - Matlab 中 parfor 循环优于 for 循环的具体例子

php - 将时间戳转换为 .ics iCal 兼容的日期格式