jQuery Makes Parsing XML Easy: jQuery, JavaScript, XML

jQuery Makes Parsing XML Easy

Tags: jQuery, JavaScript, XML | Written before Dec, 2007

I am building a Google Maps project and jQuery is making my life so much easier when parsing XML.

Regular JavaScript XML Parsing

JavaScript:
  1. var xmlDoc = request.responseXML;
  2. try // Build Markers, if available
  3. {
  4.     var markers = xmlDoc.getElementsByTagName("marker") ;
  5.     for ( var i = 0; i < markers.length ; i++ )
  6.     {
  7.         var point = {
  8.             markers[i].getAttribute("lat")),
  9.             markers[i].getAttribute("lng")
  10.         };
  11.     }
  12. } catch(e) {}

jQuery XML Parsing

JavaScript:
  1. $(request.responseXML).find("marker").each(function() {
  2.     var marker = $(this);
  3.     var point = {
  4.         marker.attr("lat"),
  5.         marker.attr("lng")
  6.     };
  7. });

The jQuery code is so much easier to read and understand. This is a basic example, but imagine when things get complex. After writing a few complex statements, you will realize the jQuery code will still be understandable, where as the JavaScript code will become hard to maintain. Thank you jQuery for making my job easier and more fun.

Comments

#1. Forrest on 7/11/07
Wow! *quickly re-writes own code excitedly*
#2. Ryan on 7/11/07
One of the best things about ActionScript 3 is E4X syntax where this stuff is really easy. I'm looking forward to when JavaScript gets that upgrade and no JavaScript trickery is needed.
#3. Marc on 7/11/07
Until then, jQuery is our upgrade to JavaScript. And it does a dang good job at it.
#4. yitz on 25/12/07
thanks, this post was the decisive one in helping me to achieve successful xml parsing in jquery.. it was the perfect tutorial :)
#5. Mustafa on 22/3/08
thank for good view about jquery, I will start it using

Leave a Comment