如何使用 jquery(或其他 javascript)解析 iTunes 格式的播客 XML 提要中的数据?

发布于 2024-11-30 07:22:43 字数 5513 浏览 0 评论 0原文

我在 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 &amp; Spirituality">
        <itunes:category text="Christianity" />
    </itunes:category>
    <category>Spirituality</category>
    <itunes:category text="Religion &amp; Spirituality">
        <itunes:category text="Spirituality" />

    </itunes:category>
    <category>Non-Profit</category>
    <itunes:category text="Government &amp; 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&apos;t just about being busy all the time-- we need to take time to sit at Jesus&apos; 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>

我一直在玩的演示的链接是 此处

就像我提到的,我希望仅从列表中提取最近更新的播客(第一个“项目”)。我显然不知所措!我希望得到一些指导。谢谢吨。


好吧,我已经成功了……有点。我将其附加到“正文”,但我想将其分配给特定的 DIV,就像原始示例一样。当我尝试将 link.appendTo('body'); 设置为 link.appendTo('podcast'); 时,我没有得到任何输出。 编辑:我想出了那部分。不过,仍然需要解决下一个问题的帮助。

其次,我希望能够在链接下放置该剧集的描述(XML 文件中的“itunes:subtitle”)。我们能让这一切发生吗?

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 able to have a description of the episode ("itunes:subtitle" in the XML file) placed under the link. Can we make this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-12-07 07:22:43

尝试一下:

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/播客.xml

<a href="http://www.deepwaterchurch.com/podcast/Jen%20Ochej-August%2014%2C%202011.mp3">Hurry Up and Wait - Slow Down</a>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文