dongshi9526 2014-02-01 21:05
浏览 32
已采纳

从ajax请求中提取内容

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.");
}
});
  • 写回答

1条回答 默认 最新

  • duang5049 2014-02-01 21:37
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求计算赫斯特(Hurst)指数
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大