Ryan Ryan - 3 days ago 7
Javascript Question

How to parse xml attributes with jQuery alone?

I'm already parsing xml successfully but i'm stuck at getting an attribute of childrens.

XML Example:

<entries>
<entry>
<media:thumbnail url="blah" />
</entry>
</entries>


Javascript/ jQuery:

$.get('data.xml', function(d){
$(d).find('entry').each(function(){
var $entry = $(this);
var pic = $entry.find('media:thumbnail').attr('url');
})
});


That javascript doesn't work for me to get an attribute.
What's the problem?

Answer

Aah, namespaces are a different kind of animal, it wasn't in your original post. You have to escape the : in your selector.

var pic = $entry.find('media\\:thumbnail').attr('url');

http://jsfiddle.net/JSrJe/1/

See also jQuery XML parsing with namespaces

Comments