Reputation: 199
I am trying to extract content from ajax request. I am able to extract some information but am having problem with others.
Below is an example of some of the requested information:
<item>
<title>Title</title>
<pubDate>Sat, 01 Feb 2014 12:12:12 GMT</pubDate>
<description>Description</description>
<enclosure url="http://www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/example.mp3" type="audio/mpeg" />
<itunes:duration>1:35:25</itunes:duration>
</item>
The Desired output:
Title
Sat, 01 Feb 2014 12:12:12 GMT
Description
1:35:25
http://www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/example.mp3
So far I have been able to extract the title, pubDate and description. I have been unsuccessful with the url and duration. I have tried different methods but have been unsucessful. I hope you can help me find a solution and thank you for your time.
Below is my code:
$.ajax({
type: "GET",
url: "php/podcast.php",
dataType: "xml",
success: function(xml){
$(xml).find('item').each(function(){
var sTitle = $(this).find('title').text();
var sPubDate = $(this).children('pubDate').text();
var sDescription = $(this).find('description').text();
var sDuration = $(this).find('itunes:duration').text();
var sURL = $(this).text().match('podtrac');
$('#rtpodcast').append('<div id="podcast"><div id="podTitle">'+ sTitle +'</div><div id="podPubDate">'+ sPubDate +'</div><div id="podDescription">'+ sDescription +'</div><div id="podDuration">'+ sDuration +'</div><div id="podURL">'+ sURL +'</div></div>');
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
Upvotes: 0
Views: 155
Reputation: 1043
This will match the text between the tags and not attributes.
example(Fiddle):
var xml = $.parseXML('<a id="text_id">text</a>');
alert($(xml).find('a').text()); // this will alert "text"
alert($(xml).find('a').attr("id")); //this will alert "text_id"
To retrive url you need to replace this
var sURL = $(this).text().match('podtrac');
with
var sURL = $(this).find("enclosure").attr("url");
For tag with namespace, try escaping colon with //
in your selector
Upvotes: 2