jQuery Makes Parsing XML Easy
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.
28 comments
Wow! quickly re-writes own code excitedly
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.
Until then, jQuery is our upgrade to JavaScript. And it does a dang good job at it.
thanks, this post was the decisive one in helping me to achieve successful xml parsing in jquery.. it was the perfect tutorial :)
thank for good view about jquery, I will start it using
thanks! that was super easy!! No ‘for’ loops or ‘getElementsByTagName’
Wont work on IE 7..
Depends on how you fetch the XML. I was using the Google XML loader and it worked fine in IE6+
Never thought could be so much simple!
It’s very useful !!!!
Fabio Rodrigues,
www.jmssolucoes.com.br
Hi all,
Thank you for the example its very useful. And its definitely much easier than parsing with simple javascript.
though I have a question: Is there any tutorial that goes in more details??
Best regards,
Nassim
Thanks, saved my bacon. I promoted you on the googlator :)
Definitely! using it now for a web based URL lookup tool, loads of xml responses – this speeded up development time and saved myself from any brain damage! :)
How do I get this working on IE7, what is the Google XML loader, I couldnt find anything about it on the web… I using my app’s generated raw XML and it doesnt work on IE =(
Nice, but you’re essentially replacing 12 lines of JavaScript with a whole library of it instead. That said, 99.999% of all my projects use jQuery anyway! :)
Thanks for the tip on this, I was writing out getElementByTagName and getAttribute and didn’t even think to just parse XML with JQuery.
I’m trying to figure out how to get to the Nth page in my XML this way but doesn’t seem like you can do $(xmlDoc).find(‘page’)8.attr(‘label’); I’ll keep googlin….
ok $(xmldoc).find(‘page’).eq(8).attr(‘label’);
Like someone else said above… This post got me going and I couldn’t help myself but spend a late, late night rewriting my XML parsing functions to use jQuery. Now, I am very close to full cross-browser support.
I wish I would have started using jQuery years ago.
Thanks for the simple write-up showcasing jQuery’s awesomeness!
Thanks for this post — it helped me along in the jQuery world.
Thanks! Good examples that helped me!
Great post. Thanks.
Here is a jQuery Plugin for easy XML parsing: http://www.bloggingdeveloper.com/post/Parse-XML-with-Javascript-jParse-jQuery-Plugin-for-Parsing-XML.aspx
Very useful, thanks.
I needed to parse some xml files and I found that the solution is right in my browser. jQuery makes it really easy. I prefer to use the ajax() method to retrieve the xml file, though.
Nice post! I’m looking for something like this for hours !!
Congratulations !
Hi,
Can we use jquery to get wordpress blog feed for a normal hand coded web site?
If yes what will be the modified code based on the example you have placed here?
Regards,
Danis
you can use YQL to get a feed. You can’t fetch remote XML requests natively with JavaScript.
expression was very nice thank you :)
and on success you parse to xml first:
Use the above function parseXml() only when you don’t set the datatype of the $ajax request