2

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());
}); 
1

3 Answers 3

3

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());
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much ps small typo should be children() instead of children
0
var xml=' <items><name>234</name> <email></email><phone></phone></items>';

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

1 Comment

find('items') doesn't, but filter('items') does, in case you care
0

it should be:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.