2

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.

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

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.

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

0

1 Answer 1

7

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

        });
    });
});

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.

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.

1
  • 2
    No one got "blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."?
    – kennsorr
    Jul 5, 2019 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.