使用jQuery和Ajax解析XML RSS提要

时间:2022-04-06 01:05:28

I am trying to parse an xml RSS feed (http://blog.counter-strike.net/index.php/feed/), currently I can get the content contained within <link> and <description> however I can't get <title> because whenever I try to get it instead of giving me the blog title, I'm given the title for my own webpage.

我正在尝试解析一个xml RSS提要(http://blog.counterstrike.net/index.php/feed/),目前我可以获得 中的内容,但是我不能获得 ,因为每当我试图获取它而不是给我博客标题时,我都会得到我自己网页的标题。</p>

The thing I find odd is that eventually if I keep refreshing the page the actual title of the blog appears. Not sure what causes this but ideally I'd prefer just to have the title of the blog and not of my own webpage.

我觉得奇怪的是,如果我不断刷新页面,博客的实际标题就会出现。不知道是什么原因造成的,但理想的情况下,我更喜欢的只是博客的标题,而不是我自己的网页。

I've looked at this question which helped me get started: parsing xml using jquery and ajax

我研究了这个问题,它帮助我开始:使用jquery和ajax解析xml

Below is the code I have so far

下面是我目前的代码。

$(document).ready(function () { $.ajax({ url: 'http://cors.io/?u=http://blog.counter-strike.net/index.php/feed/', type: 'GET', dataType: "xml" }).done(function(xml) { $.each($("item", xml), function(i, e) { $("#feed").append($(e).find("item title")); $("#feed").append("<br />") $("#feed").append($(e).find("description")); $("#feed").append("<br />") $("#feed").append($(e).find("link")); $("#feed").append("<br />") }); }); });

美元(文档)。准备好(函数(){ $。ajax({url: 'http://cors.io/?u=http:// blog.counterstrike.net/index.php/feed/',类型:'GET',数据类型:"xml"})。每个($(“项目”,xml)、功能(i,e){ $(" #食”).append($(e)。找到(“项目标题”));$(" #饲料”)。追加(“< br / >”)(“#食”).append($(e)(“描述”));$(" #饲料”)。追加(“< br / >”)(“#食”).append($(e)(“链接”));$(" #饲料”)。追加(“< br / >”)});});});

In addition to retrieving the title of the blog, I'd appreciate it if someone could tell me how to replace the likes of &#8217; which is shown within the description with what they're supposed to be.

除了检索博客的标题之外,如果有人能告诉我如何替换#8217之类的东西,我将不胜感激;描述中显示的是它们应该是什么。

Finally is it possible to retrieve and display images and videos from the RSS feed? It seems like they're contained within <content:encoded>.

最后,是否可以从RSS提要中检索和显示图像和视频?它们似乎包含在 中。

1 个解决方案

#1


3  

I'm not overly sure why I was getting the website title and not the RSS item title but I eventually got this working. Here's the code below which I used to retrieve both the title and url of the RSS item.

我不太清楚为什么我会得到网站的标题而不是RSS条目的标题,但我最终让它起作用了。下面是我用来检索RSS条目标题和url的代码。

$(document).ready(function () {
    $.ajax({
        url: 'http://cors.io/?u=http://blog.counter-strike.net/index.php/feed/',
        type: 'GET',
        dataType: "xml"
    }).done(function(xml) {

        $.each($("item", xml), function(i, e) {

            var blogNumber = i + 1 + ". ";

            var itemURL = ($(e).find("link"));
            var blogURL = "<a href='" + itemURL.text() + "'>" + itemURL.text() +"</a>";

            var itemTitle = ($(e).find("title"));
            var blogTitle = "<h4>" + blogNumber + itemTitle.text() + "</h4>";

            $("#feed").append(blogTitle);
            $("#feed").append(blogURL);

        });
    });
});

I found it easier to debug what I was retrieving from the RSS item by assigning the values I was receiving to a variable which I could then check the value of using the Google Chrome DevTools.

我发现通过分配给一个变量的值来调试从RSS项中获取的内容更容易,然后我可以检查使用谷歌Chrome DevTools的值。

I decided against getting the description and media files like images / videos from the RSS feed and instead just showed the title and url in a list in my web page. If anyone else wants to provide an answer of how to get the description and the media files working correctly, I'll be happy to approve it as the solution.

我决定不从RSS提要中获取图像/视频之类的描述和媒体文件,而是在我的web页面中显示标题和url。如果有人想提供如何使描述和媒体文件正常工作的答案,我很乐意批准它作为解决方案。

Hopefully this will help anyone who has faced similar issues.

希望这能帮助那些遇到类似问题的人。

#1


3  

I'm not overly sure why I was getting the website title and not the RSS item title but I eventually got this working. Here's the code below which I used to retrieve both the title and url of the RSS item.

我不太清楚为什么我会得到网站的标题而不是RSS条目的标题,但我最终让它起作用了。下面是我用来检索RSS条目标题和url的代码。

$(document).ready(function () {
    $.ajax({
        url: 'http://cors.io/?u=http://blog.counter-strike.net/index.php/feed/',
        type: 'GET',
        dataType: "xml"
    }).done(function(xml) {

        $.each($("item", xml), function(i, e) {

            var blogNumber = i + 1 + ". ";

            var itemURL = ($(e).find("link"));
            var blogURL = "<a href='" + itemURL.text() + "'>" + itemURL.text() +"</a>";

            var itemTitle = ($(e).find("title"));
            var blogTitle = "<h4>" + blogNumber + itemTitle.text() + "</h4>";

            $("#feed").append(blogTitle);
            $("#feed").append(blogURL);

        });
    });
});

I found it easier to debug what I was retrieving from the RSS item by assigning the values I was receiving to a variable which I could then check the value of using the Google Chrome DevTools.

我发现通过分配给一个变量的值来调试从RSS项中获取的内容更容易,然后我可以检查使用谷歌Chrome DevTools的值。

I decided against getting the description and media files like images / videos from the RSS feed and instead just showed the title and url in a list in my web page. If anyone else wants to provide an answer of how to get the description and the media files working correctly, I'll be happy to approve it as the solution.

我决定不从RSS提要中获取图像/视频之类的描述和媒体文件,而是在我的web页面中显示标题和url。如果有人想提供如何使描述和媒体文件正常工作的答案,我很乐意批准它作为解决方案。

Hopefully this will help anyone who has faced similar issues.

希望这能帮助那些遇到类似问题的人。