How to get media:thumbnail from XML Feed with JS

wwl777

Junior Member
I found out that the code I currently use to select media:thumbnail from an xml feed works in Firefox and IE9, but it isn't working in Chrome (returns undefined).

The code I use:
Code:
itemXML.getElementsByTagName('media:thumbnail').length

Code like this doesn't give any problems:
Code:
itemXML.getElementsByTagName('enclosure').length

So it must be because of the ':' sign between 'media' and 'thumbnail'. I've tried backslashing it and some other things but haven't got it working yet it chrome. I'm using Google Feed API for grabbing the xml feeds.

Has anyone got an idea how to do this properly?
 
I just found out myself, I had to know a little about xml namespaces before I could understand. With getElementsByTagNameNS you can select the name behind the colon.

code I came up with:
Code:
thumburl = itemXML.getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'thumbnail')[0].getAttribute('url');
For anyone who had the same Q:
The first part (itemXML.getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'thumbnail')) is to grab the tag with thumbnail. After that I select the first match and get the url attribute.
Media was the namespace. You have to add a URI like search.yahoo.. to give the namespace a unique name. In my xml doc I found xmlns:media="http://search.yahoo.com/mrss/" so I needed to add that as it's identifier.
 
Back
Top