jQuery Makes Parsing XML Easy[转]

  1. 云栖社区>
  2. 博客>
  3. 正文

jQuery Makes Parsing XML Easy[转]

ghost丶桃子 2016-05-18 16:29:50 浏览590
展开阅读全文

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: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/12/29/1635159.html

网友评论

登录后评论
0/500
评论
ghost丶桃子
+ 关注