17

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?

3
  • 1
    your js is correct. the problem has to be something else. Commented Apr 21, 2011 at 17:04
  • If you use console.log(d); what do you see? Commented Apr 21, 2011 at 17:09
  • Works just fine for me. Is your response content-type correct? Commented Apr 21, 2011 at 17:10

3 Answers 3

24

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

Sign up to request clarification or add additional context in comments.

1 Comment

Now that's what im talking about, Thanks a ton!!
1

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");}
    }
})

Comments

0
  $.get('data.xml', function(d) {
    $(d).find('entry').each(function() {
        var $entry = $(this);
        var pic = $e`enter code here`ntry.find('media\\:thumbnail, thumbnail').attr('url');
    })
});

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.