且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery Makes Parsing XML Easy[转]

更新时间:2022-09-21 13:07:19

jQuery Makes Parsing XML Easy

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

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!



















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2009/12/29/1635159.html,如需转载请自行联系原作者