以下是如何处理 Atom 提要的示例。在此示例中,我正在获取本地 XML 提要文件。在现实世界中,您将需要一个简单的代理脚本来为您获取它,因为您可以发出跨域 XML 请求。简而言之,要使用 jQuery 处理任何 XML,您只需使用它们的“标签”名称遍历节点集合并获取它们的内容,然后您可以根据需要重新调整它们的用途......
在这种情况下,我正在处理包含标题和内容标签的提要...对于摘要提要,您可能需要包含摘要标签处理
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript">
</script>
<script>
//This example shows getting a local ATOM file. I am assuming that you will be using a proxy to fetch the feed as you
//are getting it from a remote source
//get the feed
$.get("feed.xml", function(data){
//if XML loaded successfully find all blog entries
html = "";
$(data).find("entry").each(function(){
//get text for title and the content
title = $(this).find("title").text();
content = $(this).find("content").text()
//create your own html
html += "<h1>" + title + "</h1>";
html += "<div class='blogEntry'>" + content + "</div>"
})
//append html to the container of yor choice
$(".blogClone").append(html)
})
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<div class="blogClone">
</div>
</body>
</html>
如果您在服务器上使用 PHP,这是一个简单的代理脚本,您将需要
<?php
// PHP Proxy
// Responds to both HTTP GET and POST requests
//
// Author: Abdul Qabiz
// March 31st, 2006
//
// Get the url of to be proxied
// Is it a POST or a GET?
$url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];
$headers = ($_POST['headers']) ? $_POST['headers'] : $_GET['headers'];
$mimeType = ($_POST['mimeType']) ? $_POST['mimeType'] : $_GET['mimeType'];
//Start the Curl session
$session = curl_init($url);
// If it's a POST, put the POST data in the body
if ($_POST['url']) {
$postvars = '';
while ($element = current($_POST)) {
$postvars .= key($_POST).'='.$element.'&';
next($_POST);
}
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postvars);
}
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, ($headers == "true") ? true : false);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($session);
if ($mimeType != "") {
// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: ".$mimeType);
}
echo $response;
curl_close($session);
?>