我在think2loud.com上找到了演示如何使用jquery来解析XML文件的演示。我已下载源文件并将其上传到我自己的服务器,它工作正常。我试图将它应用到我自己的xml文件(iTunes播客源)时,我没有得到任何输出。我想,我有点过头了。
我想要的是以下列格式显示 仅第一个 播客信息:
的 的< a href =文件路径。 mp3>消息标题< / a>
消息说明
其中:
GUID是文件的路径
TITLE是消息标题
ITUNES:SUBTITLE是消息描述
以下是我想调整的演示代码示例:
<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>
这里是我要解析的XML文件的摘录:
<?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>
我一直在玩的演示的链接是here
就像我提到的那样,我希望从列表中仅提取最近更新的播客(第一个“项目”)。我显然在我的头上!我很感激一些指导。谢谢你。
好的,我已经开始工作......有点儿。我将它附加到“正文”,但我想将它分配给特定的DIV,就像原始示例一样。当我尝试将link.appendTo('body');
设置为link.appendTo('podcast');
时,我没有输出。 编辑:我认为那部分了。但是,仍然需要帮助解决下一个问题。
其次,我希望能够对链接下的剧集(XML文件中的“itunes:subtitle”)进行描述。我们可以做到这一点吗?
答案 0 :(得分:0)
试一试:
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')
}
);
我现在为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>