gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:35:06 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com