2010-09-07 28 views
0

XML/RSS源行,所以我看着這個刪除CDATA:Using jQuery to extract CDATA in XML for use as HTML content從使用jQuery

但它並沒有與我在做什麼幫助。我從網址獲取rss/xml供稿。我在標題和說明標記中遇到了CDATA問題。這裏的飼料項目:

<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標記,這樣我就可以將數據內容了呢?

非常感謝!

回答

1

其實我只是試了一下,它的工作。我添加.text()標題和鏈接變量,如下所示:

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

它工作得很好。

謝謝!

+0

是的。就是這樣:) Jquery總是指向節點,所以你需要訪問文本,如果你想處理它! – 2010-10-27 11:49:29

0

也許更新最新的jQuery版本可能會解決您的問題,因爲這適用於我的版本。