开发者

javascript jquery iterate simple XML

开发者 https://www.devze.com 2022-12-19 08:30 出处:网络
I need to iterate over XML items. Sample XML: <items> <name>234</name> <email></email>

I need to iterate over XML items.

Sample XML:

<items>
    <name>234</name>
    <email></email>
    <phone></phone>
    <phone2></phone2>
    <phone7>33</phone7>
</items>

I tried a lot of combinations but without any success. For 开发者_如何学运维example:

var xml=' <items><name>234</name> <email></email><phone></phone></items>'

$(xml).find('items\').each(function() {
  alert($(this).text() + ':' + $(this).value());
}); 


The trouble is that in your example, <items>...</items> is the root node — it is the xml variable. So, if you want its children, you can just do:

var xml='<items><name>234</name> <email></email><phone></phone></items>';
$(xml).children.each(function() {
  alert(this.nodeName + ':' + $(this).text());
});

And if you want the <items> node itself, you can do simply:

var xml='<items><name>234</name> <email></email><phone></phone></items>';
$(xml).each(function() {
  alert(this.nodeName + ':' + $(this).text());
});


var xml=' <items><name>234</name> <email></email><phone></phone></items>';

$(xml).find('items').each(function() {
alert(this.nodeName + ':' + $(this).text());
}); 


it should be:

$(xml).find('items').each(function(){
  var name = $(this).find('name').text();
  alert(name);
});
0

精彩评论

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

关注公众号