Using jQuery to Parse XML data

Under: Tutorial,jQuery 7 comments

I love jQuery. It made things so much easier. if you still don’t much know it, you can read about more amazing features on their official jQuery site. Also there is a easy way to parse XML data with jQuery and here it is.

First, Include jQuery

The best way to include jQuery these days is using google Ajax Library
URL for jQuery is here:
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

Load XML with jQuery

My xml file is test.xml which I got from W3C XML tutorial

$.ajax({ type: "GET",
    url: "test.xml",
    dataType: "xml",
    success: parseXml
  });

XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
 
 
</catalog>

A Function to Parse XML

function parseXml(xml)
{
  //find every Tutorial and print the author
  $(xml).find("cd").each(function()
  {
   $("#tiles").append(": " + $(this).find("title").text() + "<br />");
  });
 
}