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

标签 php xml parsing xml-parsing itunes

我在从我的 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

关于php - 使用 PHP DOM 方法读取 iTunes XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612712/

相关文章:

php - 如何使用主键日期解码json中多行数据并在php表中显示

php - 问题 : PHP Fatal error: Can't connect to local MySQL server through socket

xml - XmlBuilder.processing 可以接受更多参数吗?

javascript - 单击动态链接时填充 jQuery Mobile 页面内容

c# - 如何将字符串的特定字符解析为整数?

php - Disqus 数据与 PHP API 同步

涉及日语的PHP正则表达式

xml - XML 引用测试数据的免费公共(public)存储库

python - 如何在 Python 中读取大文件的特定部分

javascript - 解析表示 float 组的字符串