Parsing XML/RSS data with AJAX and jQuery

 

As you already probably know, AJAX is a javascript technology providing us with an easy way of reading and/or parsing data from the server without using even a single line of server-side language in most of the cases, or as w3schools says – “AJAX is the art of exchanging data with a server, and update parts of a web page – without reloading the whole page.“. Although it can freely be used without any library, when using jQuery in our project, why not trying its AJAX functions as well?

What is shown in the current post is RSS file data parsing with the jQuery.ajax() method. As a sample XML I am using a RSS file, used in my Useful links repository. As the file is with ever changing (let’s hope expanding) content, I would not show its contents here, but instead – in the demo page.
Let’s start first with the code I am actually using in the page where I am editing the content of the RSS Feeds in the Useful links repository.

About the Ajax primer above

The function above takes url and element as arguments, where url is the path to the file, which we are about to open for parsing and would be passed to url parameter of the jQuery.ajax() method. The element is used for searching a string in the parsed file, but have in mind that the logic behind my search is a bit weak, as it would not work correctly if there are two or more equal “title” strings in the “item” nodes.

A  bit over the RSS files

The obligatory nodes (tags) of a RSS file are title, link, description and at least one item for the actual content, where each item should have the title, link and description tags itself as well. More about the structure of a RSS file and other optional elements could easily be found on google. Now let’s go back to the Ajax calls.

The url parameters

What you need to know when opening files with Ajax is that the path for the URL parameter of each method is relative to the root of your server, no matter where the HTML or Javascript files actually are. Actually, you won’t be able to make an Ajax call to any other (web) server. Let’s observe for example the following structure: http://myDomainName.net/library/mainFeed.xml. To pass a relative path to the xml file as argument, you should use “/library/mainFeed.xml”.

jQuery.ajax() method parameters used in the first example

The other jQuery.ajax() parameters are type – the default one is “GET”, when also “POST”, “PUT” and “DELETE” are available, but the latter two are not supported by all the browser, according to the jquery ajax() documentation page. Setting cache parameter to false would force the browse not to cache the page and the dataType tells us how the returned data would be formatted. Success is a parameter, expecting a callback function to be executed by successful load, while callback “linked” to the error parameter would be executed by any errors loading the data. Both the functions could get data, textStatus and jqXHR as arguments, in the case above – xml argument passed to success represents the data, fetched from the file as xml object (because of the dataType: “xml”).

Traversing over XML-formatted data

Now, that was pretty much the basics, the actual parsing of the data depends entirely on you, I would only give you a few (let’s hope useful) examples. When formatted as xml, the returned data could be traversed with jQuery.find() method, as you can see from the example above. Using it with the current “items title” argument would give us a set of title tags, children of the item tags. Iterating over find(“title”) would give us pretty much the same, only with the channel tag’s title. As you can see, when formatted as xml, the data returned by the ajax call would be easily manipulated like any other proper DOM Object.

If we need to return the data, fetched by the ajax call, instead of processing it in a callback function, according to my research on the matter, one thing is obligatory, namely:

Now, lets substitute the dots above with the actual code from the demo page:

First have a look at the link variable on line 41 – the relative path used there is actually almost the same as the absolute one, which it corresponds to – “http://if-act.net/faddap/categories/javascript.xml”.

The getXML function

Now let’s go to  getXML – the function which returns the fetched data, using the xmlDoc variable. Have in mind that $(xml) on line 9, is a jQuery object, representing the xml data. As everything else in the Ajax call here is pretty much the same as the code used in the Ajax code in the first primer, let’s go a bit down the code. Already having the XML Object, hold by the xmlDoc variable on line 42, we can traverse it using standard jQuery functions for traversing DOM elements.

The recursive write function

The recursive function write, shown above is just a basic primer for iterating over RSS document with unknown structure and does not cover namespaces, additional tag attributes and so on.

More on the matter

All the jQuery’s Ajax methods and functions
More on the XMLHttpRequest Call
w3schools Ajax tutorial

 

47 Replies to “Parsing XML/RSS data with AJAX and jQuery”

  1. I do not even know how I finished up here, but I believed this submit was good. I do not recognise who you’re but definitely you are going to a well-known blogger in case you are not already 😉 Cheers!

  2. It is exactly like you read my ideas! You look as if understand a lot concerning this, really like you wrote it inside it or something. I do believe that you can do with some pics they are driving your message home somewhat, however besides that, it is good weblog..

  3. I’m extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one these days..

  4. I’d have to test with you here. Which isn’t one thing I usually do! I enjoy studying a put up that can make folks think. Also, thanks for allowing me to comment!

  5. It is really a nice and useful piece of information. I am glad that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.

  6. Hello. I absolutely required to leave a good swift commentary and inform you grasp that I’ve been reading your personal page for quite some time. Keep up the impressive work and I am going to be checking back again yet again relatively quickly.

  7. Hi there! Outstanding written piece! I am also a everyday targeted visitor in your site (like with abuser) of this web page in all honesty My spouse and i an effort. Me certainly not truly absolutely sure whether it is the suitable web-site to be able to question, yet you’ve got not any spam posts. We acquire comments every single day. Can simply everyone aid me? Really head over heels!

  8. We are a group of volunteers and opening a new scheme in our community. Your website provided us with valuable information to work on. You have done a formidable job and our whole community will be grateful to you.

  9. I simply want to tell you that I am very new to blogging and site-building and seriously savored you’re blog site. Likely I’m planning to bookmark your blog post . You definitely come with fabulous posts. Bless you for sharing your blog site.

  10. I want to get across my love for your kindness supporting persons that need help with this one subject matter. Your personal dedication to passing the solution up and down ended up being certainly productive and has really enabled girls much like me to achieve their aims. Your entire interesting suggestions denotes a whole lot a person like me and a whole lot more to my office colleagues. Thanks a lot; from all of us.

  11. I’m impressed, I have to say. Actually hardly ever do I encounter a blog that’s both educative and entertaining, and let me let you know, you could have hit the nail on the head. Your thought is excellent; the problem is one thing that not sufficient individuals are speaking intelligently about. I’m very joyful that I stumbled throughout this in my seek for one thing referring to this.

  12. I have read a few good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to create such a wonderful informative site.

Leave a Reply

Your email address will not be published. Required fields are marked *