使用JQuery解析WordPress RSS Feed

时间:2013-04-20 00:10:00

标签: jquery xml rss

我正在使用JQuery来读取和解析自托管的WordPress原子提要。以下是WordPress生成的XML示例:

<?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代码,用于使用此Feed并显示它:

$.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,所以我没有向我展示失败的尝试。

1 个答案:

答案 0 :(得分:0)

在您的代码中使用publisheddate.toTimeString(),这会产生错误。

为了让你的catgeories使用类似的东西:

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