我正在使用 JQuery 来读取和解析自托管的 WordPress atom 提要。以下是 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&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 的工作代码,因此我没有失败的尝试向您展示。