用PHP-DOM方法读取itunesxml文件
我在从itunes XML提要获取信息时遇到了一些问题,您可以在此处查看: 我需要从每个内部用PHP-DOM方法读取itunesxml文件,php,xml,parsing,xml-parsing,itunes,Php,Xml,Parsing,Xml Parsing,Itunes,我在从itunes XML提要获取信息时遇到了一些问题,您可以在此处查看: 我需要从每个内部标记获取信息。其中一个示例如下所示: <item> <title>What to do when a viper bites you</title> <itunes:subtitle/> <itunes:summary/> <!-- 4000 Characters Max ******** -->
标记获取信息。其中一个示例如下所示:
<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>
当毒蛇咬你时该怎么办
Phil Buechler私人秘书
61bc701c-b374-40ea-bc36-6c1cdaae8042
2012年7月22日星期日19:30:00+1100
40:01
敬拜,到达,建造,圣灵,敬拜,C3卡林福德
现在我已经取得了一些成功强> 我已经能够从中获得标题:
<?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 />';
};
?>
但我似乎无法从中得到任何其他东西。。。我对这一切都不熟悉
因此,我需要的是:
值
标记中的url属性值有人能帮我把这两个值拿出来吗?你可以用它来做这件事,让你的生活更轻松:
$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
你可以用它来做这件事,让你的生活更轻松:
$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私人秘书
上帝对彼此宽恕的呼唤——理查德·博塔
上帝对福音派上午和下午的呼召---罗伯·沃
上帝对彼此相爱的呼唤——罗伯·沃
希望这有帮助 是的,您可以使用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私人秘书
上帝对彼此宽恕的呼唤——理查德·博塔
上帝对福音派上午和下午的呼召---罗伯·沃
上帝对彼此相爱的呼唤——罗伯·沃
希望这有帮助 您可以使用children
$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";
}
?>
您可以使用children
$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";
}
?>
为什么是DOMDocument而不是SimpleXML?为什么是DOMDocument而不是SimpleXML?这很好-但我也需要附件选项卡中的url属性。这很好-但我也需要附件选项卡中的url属性。@BenPotter-“遇到问题”不是错误消息。出什么事了?哈哈,对不起,现在是早上。我可以在我的站点上复制并粘贴您的代码,但我需要设置$xml变量。当它位于以下位置时,如何执行此操作:?我试过这样做,但没有成功:$xml=simplexml\u load\u file(“);Set$xml=file\u get\u contents(“)http://www.c3carlingford.org.au/podcast/C3CiTunesFeed.xml);
太好了,效果非常好-你介意为了其他旁观者而编辑你的答案吗?谢谢你的帮助!@BenPotter-“有麻烦吗?”“不是错误消息。出什么事了?哈哈,对不起,现在是早上。我可以在我的站点上复制并粘贴您的代码,但我需要设置$xml变量。当它位于以下位置时,如何执行此操作:?我试过这样做,但没有成功:$xml=simplexml\u load\u file(“);Set$xml=file\u get\u contents(“)http://www.c3carlingford.org.au/podcast/C3CiTunesFeed.xml);
太好了,效果非常好-您介意为了其他旁观者而编辑您的答案吗?谢谢您的帮助!