Subscribe to Jack of All Trades Web Development

jQuery Makes Parsing XML Easy

JQuery, JavaScript, XML

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

Regular JavaScript XML Parsing

var xmlDoc = request.responseXML;
try // Build Markers, if available
{
	var markers = xmlDoc.getElementsByTagName("marker") ;
	for ( var i = 0; i < markers.length ; i++ )
	{
		var point = {
			markers[i].getAttribute("lat")),
			markers[i].getAttribute("lng")
		};
	}
} catch(e) {}

jQuery XML Parsing

$(request.responseXML).find("marker").each(function() {
	var marker = $(this);
	var point = {
		marker.attr("lat"),
		marker.attr("lng")
	};
});

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 Nov 07 2007

    Wow! *quickly re-writes own code excitedly*

  • #2. Ryan on Nov 07 2007

    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 Nov 07 2007

    Until then, jQuery is our upgrade to JavaScript. And it does a dang good job at it.

  • #4. yitz on Dec 25 2007

    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 Mar 22 2008

    thank for good view about jquery, I will start it using

Post a Comment!