tags:

views:

526

answers:

1

how to loop over all tags in a xml

i have a php that generates xmls like the next one

<register>
  <name>peter</name>
  <age>12</age>
</register> 
<register>
  <name>mary</name>
  <age>20</age>
</register>

so i receive this xml (this works fine)

$.ajax({success: function(xml) {  

   $(xml).find('register').each (function() 
   { 
     alert($(this).find('name').text()) // works fine, shows peter then mary on the next loop of "each"



     // But if i dont know the tag names (name,age) for each register ?     

     // Something like

      $(this).nodes().each .... // 
 alert($(this).tagName);  // i wanna show "name" & "age", how can i get the tag names inside each register in my xml sample tree?

   });    

}});
A: 

You'll want to look at the children() function, among others. See the jQuery traversal documentation for more info on the functions.

jtbandes
thanks for your fast asnwer but still i cant do it =( (and im sad ...)in the jquery help says var $kids = $(e.target).children();but still .. that prolly gives me some object/array with the childs but how do i get the tagName of each children ?this didnt work$(this).children().each(function() {alert($(this).tagName);}); // didnt workthis neither, shows all methods/att var childs = $(this).children(); for(key in childs) alert(key);
dedoz
finally ..var $childs = $(this).children(); var length = $childs.length; while(length--) alert($childs[length].tagName); thanks you very much
dedoz