开发者

Jquery - iterate through all xml tags

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);开发者_Python百科  // i wanna show "name" & "age", how can i get the tag names inside each register in my xml sample tree?

   });    

}});


This link provides a good example for use of iterating through xml

xml.find('result').find('permissionDetails').each(function(){
    $(this).children().each(function(){
        var tagName=this.tagName;
        var val=$(this).text();
        if(val==1){
            $('input:checkbox[name='+tagName+']').attr('checked',true);
        }
        else if(val==0){
            $('input:checkbox[name='+tagName+']').removeAttr('checked');
        }
    })
});

http://anasthecoder.blogspot.in/2012/02/looping-through-xml-with-jquery.html


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜