No worries on the XML vs. JSON. It's been interesting to watch your progress
in refactoring the code. I hope it's useful for other people too.

A few notes on the latest version...

* Avoid using ALLCAPS or PARTIALcaps in a variable name. Many JavaScript
programmers will think you intend such variables to be used as "constants".
(That is, a variable whose content is not intended to be changed.)

* You don't need all those makeArray calls.

* It's a bit confusing to mix the use of $ and jQuery in the same code. Use
one or the other consistently.

* Stop appending to $('div')!!!!!!!! Very bad habit! What happens when you
add another DIV to your page? Use a $('#container') selector instead.

* Even though you're still doing XML parsing, you will get much faster
performance by building an HTML string, and make sure the entire HTML
content is enclosed in one wrapper DIV.

* Watch out for all the duplicate $(something) selectors. If you use the
same $(something) more than once, than do this instead:

    var $something = $(something);
    $something.whatever();  // instead of $(something).whatever();

Putting those tips together, you get something like this:

function parseXml( xml ) {
    var html = [];
    html.push( '<div>' );
    $(xml).find('sites').each( function() {
        $(this).find('>element').each( function( i, parent ) {
            var $parent = $(parent);
            html.push( '<br/>', $parent.attr('label'), i + 1, '<br/>' );
            $parent.find('>element').each( function( j, child ) {
                var $child = $(child);
                html.push( $child.attr('label'), '<br/>' );
                $child.find('>element').each( function( k, inner ) {
                    var $inner = $(inner);
                    html.push( $inner.attr('label'), ': ', $inner.text(),
'<br/>' );
                });
            });
        });
    });
    html.push( '</div>' );

    $('#container').append( html.join('') );
}

-Mike

On Tue, Feb 2, 2010 at 4:10 PM, augur <312...@gmail.com> wrote:

> function parseXml(xml) {
>                $(xml).find('sites').each(function(){
>                var PARENTarr = jQuery.makeArray($(this).find('>element'));
>                         $(PARENTarr).each(function(i){
>                                  $("div").append("<br/>"+
> $(this).attr("label")+(i+1) +"<br/>");
>                                  var CHILDarr =
> jQuery.makeArray($(PARENTarr[i]).find
> ('>element'));
>                                  $(CHILDarr).each(function(p){
>
>  $("div").append($(this).attr("label") +"<br/>");
>                                                var CHILDattrs =
> jQuery.makeArray($(CHILDarr[p]).find
> ('>element'));
>
>  $(CHILDattrs).each(function(){
>                                                          var CHILDid =
> $(this).attr('label') +": "+ $(this).text();
>
>  $("div").append(CHILDid +"<br/>");
>                                                           p=0;
>                                                });
>                                   });
>                          });
>                  });
>        }
> });

Reply via email to