Magic Studio

专心做有逼格的APP!

jQuery Makes Parsing XML Easy[转]

jQuery Makes Parsing XML Easy

Category: JavaScript & jQuery 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

Copy Codeblock to Clipboard

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

Copy Codeblock to Clipboard

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.

posted on 2009-12-29 15:45  Mr 布鲁斯  阅读(206)  评论(0编辑  收藏  举报

导航