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&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,所以我沒有我的失敗嘗試留給你看。
完美!謝謝你的幫助。我一定很親密,因爲你的代碼似乎很熟悉。不知何故,它只是沒有工作,雖然.... – brilang 2013-04-22 05:46:20