Menu
I am writing a script that reads a photofeed from Flickr. I’m having trouble extracting information from certain tags.
For example, this XML and JS works:
[CODE]<title>alium hands!</title>
[CODE]itemTitle = getElementsByTagName(‘title’)[0].firstChild.nodeValue;
document.write(itemTitle);
But this does not:
[CODE]<media:title>alium hands!</media:title>
[CODE]itemMediaTitle = getElementsByTagName(‘media:title’)[0].firstChild.nodeValue;
document.write(itemMediaTitle);
I know that it thinks the tag is a prefix and local name, but I don’t know what code will make it work. Any suggestions?
var media = "whatever_namespace_uri_is_assigned_to_the_media_prefix"
var titles = theDocument.getElementsByTagNameNS(media, "title")
Or, if there are no other elements with the local name 'title', the slightly cheating shortcut var titles = theDocument.getElementsByTagNameNS("*", "title")
[CODE]var titles = theDocument.getElementsByTagNameNS(media, "title")
var title = titles[0].nodeValue;[/CODE]
<i>
</i>(function(){
try{
var t=XMLDocument.prototype.getElementsByTagName;
XMLDocument.prototype.getElementsByTagName=function (s){
var a=new String(s).split(":");
if(a.length>2)return null;//Reject "double" name spaces.
if(a.length==1)return t.apply(this,[s]);
var r=t.apply(this,["*"]),l=r.length;
var p=new Array();
for(var i=0;i<l;i++){
if(r[i].prefix==a[0] && r[i].localName==a[1])p[p.length]=r[i];
}
return p;
}
}catch(e){};
})()
<i>
</i>itemMediaTitle = xmldoc.getElementsByTagName('media:title')[0].firstChild.nodeValue
document.write(itemMediaTitle);
<i>
</i>(function(){
try{
var t=XMLDocument.prototype.getElementsByTagName;
XMLDocument.prototype.getElementsByTagName=function(s){
var a=new String(s).split(":");
if(a.length>2)return null;//Reject "double" name spaces.
if(a.length==1)return t.apply(this,[s]);
var r=t.apply(this,["*"]),l=r.length;
var p=new Array();
for(var i=0;i<l;i++){
if(r[i].prefix==a[0] && r[i].localName==a[1])p[p.length]=r[i];
}
return p;
}
}catch(e){};
try{
var t=Element.prototype.getElementsByTagName;
Element.prototype.getElementsByTagName=function(s){
var a=new String(s).split(":");
if(a.length>2)return null;//Reject "double" name spaces.
if(a.length==1)return t.apply(this,[s]);
var r=t.apply(this,["*"]),l=r.length;
var p=new Array();
for(var i=0;i<l;i++){
if(r[i].prefix==a[0] && r[i].localName==a[1])p[p.length]=r[i];
}
return p;
}
}catch(e){};
})()
<i>
</i>(function(){
try{
var t=XMLDocument.prototype.getElementsByTagName;
XMLDocument.prototype.getElementsByTagName=function(s){
var a=new String(s).split(":");
if(a.length>2)return null;//Reject "double" name spaces.
if(a.length==1)return t.apply(this,[s]);
var r=t.apply(this,["*"]),l=r.length;
var p=new Array();
for(var i=0;i<l;i++){
if(r[i].prefix==a[0] && r[i].localName==a[1])p[p.length]=r[i];
}
return p;
}
}catch(e){};
try{
var t2=Element.prototype.getElementsByTagName;
Element.prototype.getElementsByTagName=function(s){
var a=new String(s).split(":");
if(a.length>2)return null;//Reject "double" name spaces.
if(a.length==1)return t2.apply(this,[s]);
var r=t2.apply(this,["*"]),l=r.length;
var p=new Array();
for(var i=0;i<l;i++){
if(r[i].prefix==a[0] && r[i].localName==a[1])p[p.length]=r[i];
}
return p;
}
}catch(e){};
})()
<i>
</i>
<?xml version="1.0" encoding="UTF-8" ?>
- <feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gml="http://www.opengis.net/gml" xmlns:georss="http://www.georss.org/georss" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:gd="http://schemas.google.com/g/2005">
<id>http://gdata.youtube.com/feeds/api/users/Hysteria862/uploads?start-index=1&max-results=25</id>
<updated>2008-02-28T20:27:15.746Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
<title type="text">Hysteria862's Videos</title>
<logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
<link rel="related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/hysteria862" />
<link rel="alternate" type="text/html" href="http://www.youtube.com/profile_videos?user=hysteria862" />
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/Hysteria862/uploads" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/Hysteria862/uploads?start-index=1&max-results=25" />
- <author>
<name>hysteria862</name>
<uri>http://gdata.youtube.com/feeds/api/users/hysteria862</uri>
</author>
<generator version="beta" uri="http://gdata.youtube.com/">YouTube data API</generator>
<openSearch:totalResults>13</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
- <entry>
<id>http://gdata.youtube.com/feeds/api/videos/cxnKR7Tl0aI</id>
<published>2006-10-03T03:28:35.000-07:00</published>
<updated>2008-01-18T09:07:54.000-08:00</updated>
<category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="People" label="People & Blogs" />
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Nathan" />
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="wet" />
<title type="text">Nathan gets wet</title>
<content type="text">Nathan's silly faces at the camera come to an abrupt end when Mat soaks him with water.</content>
<link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=cxnKR7Tl0aI" />
<link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/cxnKR7Tl0aI/responses" />
<link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/cxnKR7Tl0aI/related" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/hysteria862/uploads/cxnKR7Tl0aI" />
- <author>
<name>Hysteria862</name>
<uri>http://gdata.youtube.com/feeds/api/users/hysteria862</uri>
</author>
- <media:group>
[color=red] <media:title type="plain">Nathan gets wet</media:title> [/color]
<media:description type="plain">Nathan's silly faces at the camera come to an abrupt end when Mat soaks him with water.</media:description>
[color=red] <media:keywords>Nathan, wet</media:keywords> [/color]
<yt:duration seconds="12" />
<media:category label="People & Blogs" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>
<media:content url="http://www.youtube.com/v/cxnKR7Tl0aI" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="12" yt:format="5" />
<media:content url="rtsp://rtsp2.youtube.com/ChoLENy73wIaEQmi0eW0R8oZcxMYDSANFEgGDA==/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="12" yt:format="1" />
<media:content url="rtsp://rtsp2.youtube.com/ChoLENy73wIaEQmi0eW0R8oZcxMYESARFEgGDA==/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="12" yt:format="6" />
[color=red] <media:player url="http://www.youtube.com/watch?v=cxnKR7Tl0aI" /> [/color]
<media:thumbnail url="http://img.youtube.com/vi/cxnKR7Tl0aI/2.jpg" height="97" width="130" time="00:00:06" />
<media:thumbnail url="http://img.youtube.com/vi/cxnKR7Tl0aI/1.jpg" height="97" width="130" time="00:00:03" />
<media:thumbnail url="http://img.youtube.com/vi/cxnKR7Tl0aI/3.jpg" height="97" width="130" time="00:00:09" />
<media:thumbnail url="http://img.youtube.com/vi/cxnKR7Tl0aI/0.jpg" height="240" width="320" time="00:00:06" />
</media:group>
<yt:statistics viewCount="201" />
- <gd:comments>
<gd:feedLink href="http://gdata.youtube.com/feeds/api/videos/cxnKR7Tl0aI/comments" countHint="0" />
</gd:comments>
</entry>
<i>
</i>var doc = http.responseXML;
var media = ="http://search.yahoo.com/mrss/";
var player = doc.getElementsByTagNameNS(media, "player")[0].getAttribute("url");
var title = doc.getElementsByTagNameNS(media, "title")[0].firstChild.nodeValue;
var keywords = doc.getElementsByTagNameNS(media,"keywords")[0].firstChild.nodeValue;
alert(keywords);
0.1.9 — BETA 5.18