使用jQuery从XML / RSS提要中删除CDATA

时间:2010-09-07 13:15:01

标签: jquery xml rss cdata

好的所以我看了这个:Using jQuery to extract CDATA in XML for use as HTML content

但它对我正在做的事情没有帮助。我从网址获取rss / xml提要。我在标题和描述标签中遇到CDATA问题。这是Feed项目:

  <item>
   <title><![CDATA[Impact South Africa (Girls)]]></title>
   <link>http://www.thriveafricastore.com/products.php?product=Impact-South-Africa-%28Girls%29</link>
   <pubDate>Mon, 29 Mar 2010 19:02:26 +0000</pubDate>
   <guid isPermaLink="false">http://www.thriveafricastore.com/products.php?product=Impact-South-Africa-%28Girls%29</guid>
   <description><![CDATA[<div style='float: right; padding: 10px;'><a href="http://www.thriveafricastore.com/products.php?product=Impact-South-Africa-%28Girls%29"  ><img src="http://www.thriveafricastore.com/product_images/t/266/girls__27047_thumb.jpg" alt="" /></a></div><p><strong>You can help us impact South Africa. Do something!<br /></strong></p>
<p>Your basic jersey tee, only better! Made just for women, it contours to your shape and always looks flattering and chic even with a simple pair of jeans. This is one t-shirt you'll want to stock pile. Pre-shrunk 1..<p><strong>Price: <span class="SalePrice">$10.00</span></strong> </p>]]></description>
   <content:encoded><![CDATA[<div style='float: right; padding: 10px;'><a href="http://www.thriveafricastore.com/products.php?product=Impact-South-Africa-%28Girls%29"  ><img src="http://www.thriveafricastore.com/product_images/t/266/girls__27047_thumb.jpg" alt="" /></a></div><p><strong>You can help us impact South Africa. Do something!<br /></strong></p>
<p>Your basic jersey tee, only better! Made just for women, it contours to your shape and always looks flattering and chic even with a simple pair of jeans. This is one t-shirt you'll want to stock pile. Pre-shrunk 1..<p><strong>Price: <span class="SalePrice">$10.00</span></strong> </p>]]></content:encoded>
  </item>

这是我到目前为止的jQuery:

   $.get('http://www.thriveafricastore.com/rss.php?type=xml', {}, function(d) {

     $('body').append('New Thrive Store Items'); 
     $('body').append('<div>');

    $('item', d).each(function() {

     var $item = $(this);
     var title = $item.find('title');
     var description = $item.find('description').text();
     var link = $item.find('link');
     var html = '<h3><a href="' + link + '">' + title + '</a></h3>';

     $('div').append($(html));

    });
   }, 'xml');

我下次停止删除CDATA标签的任何想法,以便我可以将内容拉出来?

非常感谢!

2 个答案:

答案 0 :(得分:1)

实际上我只是尝试了一些东西而且有效。我将.text()添加到标题和链接变量中,如下所示:

            var title = $item.find('title').text();
            var description = $item.find('description').text();
            var link = $item.find('link').text();

它运作得很好。

谢谢!

答案 1 :(得分:0)

对最新的jquery版本进行更新可能会解决您的问题,因为这适用于我的版本。