使用jQuery和Ajax解析XML RSS feed [英] Parsing XML RSS feed using jQuery and Ajax

查看:77
本文介绍了使用jQuery和Ajax解析XML RSS feed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析xml RSS feed( http://blog. counter-strike.net/index.php/feed/),目前我可以获取<link><description>中包含的内容,但是我无法获取<title>,因为无论何时我尝试获取它给我博客标题的标题,就是我自己网页的标题.

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.

我觉得奇怪的是,最终如果我不断刷新页面,就会显示博客的实际标题.不知道是什么原因导致的,但是理想情况下,我更希望仅拥有博客的标题,而不要拥有自己的网页.

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.

我研究了这个帮助我入门的问题:使用jquery解析xml和ajax

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

下面是我到目前为止的代码

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 />") }); }); });

$(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 />") }); }); });

除了检索博客的标题之外,如果有人可以告诉我如何将描述中显示的&#8217;之类的内容替换为应该显示的内容,我将不胜感激.

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.

最后可以从RSS提要中检索和显示图像和视频吗?似乎它们包含在<content:encoded>中.

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项目标题,但最终还是可以正常工作.这是下面的代码,我用来检索RSS项的标题和URL.

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.

$(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);

        });
    });
});

我发现通过将我收到的值分配给一个变量,可以更轻松地调试我从RSS项中检索的内容,然后可以检查使用Google Chrome DevTools的值.

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提要中获取描述和媒体文件,例如图像/视频,而只是在网页列表中显示标题和URL.如果其他人想提供有关如何使描述和媒体文件正常工作的答案,我很乐意将其作为解决方案.

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.

希望这将对遇到类似问题的所有人有所帮助.

Hopefully this will help anyone who has faced similar issues.

这篇关于使用jQuery和Ajax解析XML RSS feed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆