Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 668
CodeOverload Avatar asked Apr 21 '11 17:04

CodeOverload


2 Answers

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

like image 94
DarthJDG Avatar answered Oct 11 '22 07:10

DarthJDG


try this out

$.ajax({
    type: "GET",
    url: 'data.xml,
    dataType: "xml",
    success: function(xml) {
        $(xml).find('entry').each(function(){
            var $entry = $(this);
            var pic = $entry.find('picture').attr('url');
            alert(pic);
        })
    },
    error: function(xhr, status, error) {
        if (xhr.status != 404) {alert(error);} else {alert("404 xml not found");}
    }
})
like image 34
mcgrailm Avatar answered Oct 11 '22 05:10

mcgrailm