使用 Javascript 将 RSS 标题解析为 HTML

Parse RSS title into HTML using Javascript

我正在尝试使用 Javascript(例如 jQuery - 我们已经导入 v.1.9.1),用我们网站的 RSS 新闻源中的 5 个最新主题填充网页上的菜单,并且我一直在搜索这里的内容以找到有用的东西,但最适合我的问题的答案似乎是使用已弃用的脚本。

RSS feed存放在我们自己的服务器上,所以要保证阅读权限。我更喜欢使用某种形式的 Javascript 并且我只需要拥有例如的标题最新的 X 新闻及其链接已放入

<li><a href="Link to article">first news header</a></li>
<li><a href="Link to article">second news header</a></li>

这里的一个答案让我找到了这个 JSfiddle:Code but it doesn't seem to work? Another answer in here code 给出了一个 jQuery 代码,我不知道如何在我的 HTML 之后使用它,所以它可能就像指导我使用它一样简单?

我使用 JS 的经验非常有限,所以我非常感谢一些漂亮的 low-tech 建议,关于如何让事情在 .js 文件中包含什么以及如何让它出现方面工作在我的 HTML 之后。

新闻提要在这里:Link

谢谢!

基于 answer you found as well, I put together a jsfiddle 进行测试。 (请注意,我在 fiddle 中使用预定义的 XML 字符串,因为我无法访问域上的 RSS 提要)

代码解释如下

简单HTML:

<ul id="feed">
</ul>

Javascript:

$(document).ready(function(){

var x=10; // your X iteration limit

// load the xml data. it is parsed by jquery
$.get("http://www.flatpanels.dk/rss/nyhed.xml", function(data) {
    var $xml = $(data);

    $xml.find("item").each(function(i, val) { // find the items in the rss and loop

        // create an item object with all the necessary information out of the xml
        var $this = $(this),
            item = {
                title: $this.find("title").text(),
                link: $this.find("link").text(),
                description: $this.find("description").text(),
                pubDate: $this.find("pubDate").text(),
                author: $this.find("author").text(),
                guid: $this.find("guid").text()
        };
        // replace the CDATA in the item title
        item.title = item.title.replace("<![CDATA[", "").replace("]]>", "");

        // #feed selects the ul element with the id feed
        // and append() appends a newly created li element
        // to the ul
        $('#feed').append($('<li><a href="' +item.guid +'">' +item.title +'</a></li>'));

        return i<(x-1); // (stop after x iterations)
    });
});
});