samedi 25 avril 2015

JQuery children loop XML


Using JQuery and AJAX, I am getting an XML document and then looping through the document in an attempt to do something at each level of the XML tags/nodes, from parent to children and then children of children respectively.

What i have currently works but is specifically written to a limit of children of children of children (Not ideal).

The final application may have an open ended number of children to do the action to.

How can i make this account for any number of children without the inefficiency?

I have tried making a function with the basis of what i wish to achieve at each child level. But this does not work as expected, essentially breaking the script.

The function

function childX() {
    $(this).children().each(function()
    {
       $("#output").append("<div id='"+(this).nodeName+"'>"+(this).nodeName+"</div><br />");
    });
}

The main script

  $(xml).find("RecentTutorials").children().each(function()
  {

    $("#output").append("<div id='"+(this).nodeName+"'>"+(this).nodeName+"</div><br />");


    $(this).children().each(function()
    {
       $("#output").append("<div id='"+(this).nodeName+"'>"+(this).nodeName+"</div><br />");

      $(this).children().each(function()
        {
           $("#output").append("<div id='"+(this).nodeName+"'>"+(this).nodeName+"</div><br />");

            $(this).children().each(function()
            {
               $("#output").append("<div id='"+(this).nodeName+"'>"+(this).nodeName+"</div><br />");

            });

        });

    });


  //alert((this).nodeName);
  }); 


Aucun commentaire:

Enregistrer un commentaire