2011-02-16 90 views
0

當我解析XML時,無法獲取鏈接標記。我嘗試了幾種方法。這是我使用的是什麼現在:使用jQuery從XML中提取信息

$(document).ready(function() { 

     $('#news-feed').each(function(){ 
     var $container = $(this); 
     $container.empty();    
      $.get('blogs.xml', function(data){ 
     $('entry', data).each(function() { 
      var $link = $('<a></a>') 
      .attr('href', $('link', this).text()) 
      .text ($('title', this).text()); 
      var $headline = $('<h5></h5>').append($link);   
      var author = $(this).find('author').text(); 
      var pubDate = new Date(
      $('published', this).text()); 
      var pubMonth = pubDate.getMonth() + 1; 
      var pubDay = pubDate.getDate(); 
      var pubYear = pubDate.getFullYear(); 
      var $publication = $('<div></div>') 
       .addClass('publication-date') 
       .text('Posted by ' + author + ' on ' + pubMonth + '/' + pubDay +'/' 
       +pubYear); 

     $('<div></div>') 
      .append($headline, $publication) 
      .appendTo($container); 

      }); 
}); 

    }); 

}); 




<?xml version="1.0" encoding="utf-8"?> 
<feed 
<entry> 
     <title>Quality Aspects of Flexibility and Scalability in Distance Education Design</title> 
     <link rel="alternate" type="text/html" href="http://www.personal.psu.edu/author/blogs/instructional_design_and_higher_education/2010/09/quality-aspects-of-flexibility-vs-scalability-in-distance-education-design.html" /> 
     <id>tag:www.personal.psu.edu,2010:/author/blogs/instructional_design_and_higher_education//16140.270413</id> 

     <published>2010-09-02T13:21:31Z</published> 
     <updated>2010-09-16T12:49:51Z</updated> 
     <author> 
      <name>author</name> 

     </author> 

     <category term="3204" label="distance education" scheme="http://www.sixapart.com/ns/types#tag" /><category term="59026" label="flexible design" scheme="http://www.sixapart.com/ns/types#tag" /><category term="48647" label="wcldportfoliopilot" scheme="http://www.sixapart.com/ns/types#tag" /><category term="54688" label="wcldwebsite" scheme="http://www.sixapart.com/ns/types#tag" /> 
     <content type="html" xml:lang="en" xml:base="http://www.personal.psu.edu/author/blogs/instructional_design_and_higher_education/"> 



     </content> 
    </entry> 
</feed> 
+0

你最好使用一個簡短的(但是具體的)標題,並把實際的問題放在它所屬的地方。此外,我認爲你必須在你的問題上擴大一點(並且你的代碼的正確格式也是不錯的)。 – 2011-02-16 21:55:05

+0

XML格式錯誤(*缺少關閉>在提要標記*) – 2011-02-16 21:58:16

回答

1

您檢索鏈接的.text(),但鏈接的標籤是自封閉所以它不是文本。

使用$('link', this).attr('href')從鏈接標記的href屬性中提取URL。