views:

36

answers:

1

Basically, dealing with an XML feed that's not written in the nicest way (Youtube API data), and follows on from a previous question on the topic.

Currently, there are four nodes in the file named media:thumbnail. Each media.thumbnail node has various attributes, one of which is url. I am only wanting to retrieve value of the first occurrence of the url attribute (the result being http://i.ytimg.com/vi/CQP_AuT4zXQ/2.jpg).

Example;

<item>
...
<media:group>
...
    <media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/2.jpg' height='90' width='120' time='00:04:53.500'/>
    <media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/1.jpg' height='90' width='120' time='00:02:26.750'/>
    <media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/3.jpg' height='90' width='120' time='00:07:20.250'/>
    <media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/0.jpg' height='240' width='320' time='00:04:53.500'/>
...
</media:group>
...
</item>

My knowledge of jQuery is still developing, and I've been able to retrieve the values of the url attribute for all occurrences of media:thumbnail using the following code:

$(this).find("[nodeName=media:thumbnail]").each(function()
      {
        $("#output").append("<img src=\"" + $(this).attr("url") + "\">\n");
      });

How do I now limit this so I only grab the value of url for the first occurrence of media:thumbnail?

A: 
var elt = $(this).find("[nodeName=media:thumbnail]").first()

See also:

http://api.jquery.com/first/

EDIT

Here's an example that does exactly what you need (but with HTML instead of XML)

http://jsbin.com/ecuku4/2/edit

Cheers!

Pablo Fernandez
Sorry Pablo, but I have looked at that article and can't make sense of how to apply it to my situation. Once you've assigned the first instance of the relevant node to the variable, how do you retrieve it?Is `$("#output").append("<img src=\"" + $(elt).attr("url") + "\">\n");` acceptable or am I still barking up the wrong tree?
thewinchester
elt.attr("url") you don't need to add the dollar sign again since elt is already an jQuery augmented object
Pablo Fernandez
Edited with working example, hope that helps
Pablo Fernandez