views:

66

answers:

3

Hello

i need to iterate xml items

Sample xml

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

i tried lot of combinations but no success :( for example

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

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

any help

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

$(xml).find('items').each(function() {
alert(this.nodeName + ':' + $(this).text());
}); 
prodigitalson
find('items') will not work because items is the root node.
wsanville
`find('items')` doesn't, but `filter('items')` does, in case you care
munch
A: 

it should be:

$(xml).find('items').each(function(){
  var name = $(this).find('name').text();
  alert(name);
});
Jim Schubert
+2  A: 

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());
});
VoteyDisciple
thank you very muchps small typo should be children() instead of children
wicherqm