How can I use jquery (or other javascript) to parse data from iTunes-formatted podcast XML feed?
I found the demo at think2loud.com that shows how to use jquery to parse an XML file. I have downloaded the source files and uploaded them to my own server and it works fine. A soon as I try to apply it to my own xml file (iTunes podcast feed) I don't get any output. I'm kind of in over my head, I think.
What I'd like is to have ONLY THE FIRST podcast info displayed in the following format:
< a href = path to file . mp3 > Title of message< / a >
Description of message
where:
GUID is path to file
TITLE is Title of Message
ITUNES:SUBTITLE is Description of message
Here's a sample of the demo code I'd like to tweak:
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "sites.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('site').each(function(){
var id = $(this).attr('id');
var title = $(this).find('title').text();
var url = $(this).find('url').text();
$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
$(this).find('desc').each(function(){
var brief = $(this).find('brief').text();
var long = $(this).find('long').text();
$('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
$('<div class="long"></div>').html(long).appendTo('#link_'+id);
});
});
}
});
});
</script>
and here's an excerpt of the XML file I'd like to parse:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link rel="self" type="application/rss+xml" href="http://www.deepwaterchurch.com/podcast/podcast.xml" />
<lastBuildDate>Mon, 15 Aug 2011 13:30:32 -0300</lastBuildDate>
<title>Deep Water Podcast</title>
<itunes:author>AJ Thomas</itunes:author>
<link>http://www.deepwaterchurch.com</link>
<generator>Podcast Maker v1.4.1 - http://www.lemonzdream.com/podcastmaker</generator>
<description><![CDATA[The weekly message from Deep Water Church in Halifax, Nova Scotia.]]></description>
<itunes:subtitle />
<itunes:summary>The weekly message from Deep Water Church in Halifax, Nova Scotia.</itunes:summary>
<language>en</language>
<copyright>2007 Deep Water Church</copyright>
<image>
<url>http://www.deepwaterchurch.com/podcast/podcast_144.jpg</url>
<title>Deep Water Podcast</title>
<link>http://www.deepwaterchurch.com</link>
<width>144</width>
<height>144</height>
</image>
<itunes:image href="http://www.deepwaterchurch.com/podcast/podcast.jpg" />
<category>Christianity</category>
<itunes:category text="Religion & Spirituality">
<itunes:category text="Christianity" />
</itunes:category>
<category>Spirituality</category>
<itunes:category text="Religion & Spirituality">
<itunes:category text="Spirituality" />
</itunes:category>
<category>Non-Profit</category>
<itunes:category text="Government & Organizations">
<itunes:category text="Non-Profit" />
</itunes:category>
<itunes:keywords>deep water, deepwater, church, halifax, nova scotia, canada, jesus, aj thomas</itunes:keywords>
<itunes:explicit>no</itunes:explicit>
<item>
<title>Hurry Up and Wait - Slow Down</title>
<itunes:author>Deep Water</itunes:author>
<description><![CDATA[Episode 2 of 2. Luke 10:38-42. A life that is ministry isn't just about being busy all the time-- we need to take time to sit at Jesus' feet and be quiet, too.]]></description>
<itunes:subtitle>Episode 2 of 2. Luke 10:38-42. A life that is ministry isn't just about being busy all the time-- we need to take time to sit at Jesus' feet and be quiet, too.</itunes:subtitle>
<itunes:summary />
<enclosure url="http://www.deepwaterchurch.com/podcast/Jen%20Ochej-August%2014%2C%202011.mp3" type="audio/mpeg" length="16625879" />
<guid>http://www.deepwaterchurch.com/podcast/Jen%20Ochej-August%2014%2C%202011.mp3</guid>
<pubDate>Mon, 15 Aug 2011 13:29:03 -0300</pubDate>
<category>Christianity</category>
<itunes:explicit>no</itunes:explicit>
<itunes:duration>00:34:34</itunes:duration>
<itunes:keywords>deep water, deepwater, church, halifax, nova scotia, canada, jesus, aj thomas</itunes:keywords>
</item>
The link to the demo I've been playing with is here
Like I mentioned, I'd like this to pull only the most recently updated podcast (first "item") from the list. I'm clearly in over my head! I'd appreciate some guidance. Thanks tons.
Ok, I've got it working... kind of. I have it appending to the "body", but I'd like to assign it to a specific DIV, like the original example. When I try to set the link.appendTo('body');
to link.appendTo('podcast');
I get no output. EDIT: I figured that part out. Still need help with the next question, though.
Secondarily, I'd like to be a开发者_如何学JAVAble to have a description of the episode ("itunes:subtitle" in the XML file) placed under the link. Can we make this happen?
Give it a try:
jQuery(
function($)
{
$.get('sites.xml',
function(xml)
{
var item=$('channel > item:first',xml);
if(item.length)
{
var link=$('<a/>').attr('href',$('enclosure',item).attr('url'))
.text($('title',item).text());
//put it where you want to
link.appendTo('body');
}
},
'xml')
}
);
Returns for me right now for http://www.deepwaterchurch.com/podcast/podcast.xml
<a href="http://www.deepwaterchurch.com/podcast/Jen%20Ochej-August%2014%2C%202011.mp3">Hurry Up and Wait - Slow Down</a>
精彩评论