2013-04-20 86 views
0

我使用JQuery来读取和解析自托管的WordPress原子提要。这里是由WordPress生成的XML的例子:用JQuery解析WordPress RSS源

<?xml version="1.0" encoding="UTF-8"?> 
<feed 
    xmlns="http://www.w3.org/2005/Atom" 
    xmlns:thr="http://purl.org/syndication/thread/1.0" 
    xml:lang="en-US" 
    xml:base="http://example.com/wp/wp-atom.php" 
    > 
    <title type="text">Feed Title</title> 
    <subtitle type="text">Feed Subtitle</subtitle> 
    <updated>2013-04-19T21:33:48Z</updated> 
    <link rel="alternate" type="text/html" href="http://example.com/wp" /> 
    <id>http://example.com/wp/index.php/feed/atom/</id> 
    <link rel="self" type="application/atom+xml" href="http://example.com/wp/index.php/feed?feed=atom&#038;cat=10" /> 
    <generator uri="http://wordpress.org/" version="3.5.1">WordPress</generator> 
    <entry> 
     <author> 
     <name>Author Name</name> 
     </author> 
     <title type="html">Article Title</title> 
     <link rel="alternate" type="text/html" href="http://example.com/wp/index.php/2013/04/19/article-title/" /> 
     <id>http://example.com/wp/?p=1418</id> 
     <updated>2013-04-19T21:33:48Z</updated> 
     <published>2013-04-19T21:33:16Z</published> 
     <category scheme="http://example.com/wp" term="firstcategory" /> 
     <category scheme="http://example.com/wp" term="secondcategory" /> 
     <category scheme="http://example.com/wp" term="thirdcategory" /> 
     <summary type="html">A summary excerpt of the article [...]</summary> 
     <content type="html" xml:base="http://example.com/wp/index.php/2013/04/19/tons-vs-tonnes/">A summary excerpt of the article with a whole bunch more text taht is not in the summary. 
     </content> 
     <link rel="replies" type="text/html" href="http://example.com/wp/index.php/2013/04/19/article-title/#comments" thr:count="0"/> 
     <link rel="replies" type="application/atom+xml" href="http://example.com/wp/index.php/2013/04/19/article-title/feed/atom/" thr:count="0"/> 
     <thr:total>0</thr:total> 
    </entry> 
</feed> 

这里是我的JQuery AJAX代码来使用这种饲料,并显示它:

$.ajax({ 
    url: "//example.com/wp/index.php/feed?feed=atom&cat=10", 
    dataType: "xml", 
    success: function(data) { 
     console.log(data); 
     var $xml = $(data), 
      items = []; 

     $xml.find("entry").each(function(i) { 
      if (i === 30) return false; 
      var $this = $(this); 
      console.log($this); 

      items.push('<div><h3>' + $this.find("title").text() + '</h3><p><small>Published by ' + $this.find("author").text().trim() + ' on ' + publisheddate.toDateString() + ' ' + publisheddate.toTimeString() + '</small></h3>' + $this.find("content").text() + '</div><hr />'); 
     }); 
     $("#displayDiv").html(items.join('')); 

这是工作! 我想与所有此条分类标签的列表,以加强这方面如下:

firstcategory | secondcategory | thirdcategory 

如果你看一下XML,你会发现有在样本进入第三类元素。

我已经尝试了几种方法,并失败惨败。我已经恢复了工作代码,从中我已经提取了上面的示例JavaScript,所以我没有我的失败尝试留给你看。

回答

0

在您的代码中,您使用publisheddate.toTimeString(),它提供了一个错误。

为了让您的catgeories使用类似:

  var categories = ""; 
      $this.find("category").each(function(j) { 
       categories += " " + ($(this).attr("term")); 
      }) 
      items.push("categories:" + categories); 
+0

完美!谢谢你的帮助。我一定很亲密,因为你的代码似乎很熟悉。不知何故,它只是没有工作,虽然.... – brilang 2013-04-22 05:46:20