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
JavaScript:
-
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
JavaScript:
-
$ (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.
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/12/29/1635159.html