I am looking to parse XML data (using this URL: https://gdata.youtube.com/feeds/api/videos/ijudAdhLGmg?v=2) and get the contents of the title element and store it in a variable so I can use it later in my script. I am new to JQuery and I am not sure how to do this.
4 Answers
code below will fetch the xml from the url and get the title
$.ajax({
url:"https://gdata.youtube.com/feeds/api/videos/ijudAdhLGmg?v=2",
dataType:"xml",
crossDomain: true,
success:function(html){
var title = $(html).find("title")[0];
}
});
however the problem you might face is the cross site problem, where modern browser will not allow ajax to call an url that not the same as the host
Comments
You can check the jQuery online doc.
This one is for jQuery to parse XML: jQuery.parseXML()
Comments
you can use .psrseXML for the parsing
$.ajax({
url:'https://gdata.youtube.com/feeds/api/videos/ijudAdhLGmg?v=2',
type:'GET',
dataType:'jsonp',
success:function(data){
//console.log(data);
var xml = data,
xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc),
$mediaDes = $xml.find("media\\:description").text();
console.log($mediaDes );
}
});
the data in the success callback contains the xml
1 Comment
Rafay
@user1126921 you will have to parse the xml and append the wanted values using
append or appendTo functions you can search them