coldfusion_ColdFusion MX的内容联合

coldfusion

Content syndication has become a massively popular approach to providing content to a Website’s visitors, as well as providing a means for many people to gather information in one place for quick and easy viewing.

内容联合组织已成为一种非常流行的向网站访问者提供内容的方法,并且为许多人提供了一种在一个地方收集信息以便快速轻松查看的方法。

For example, if you get your ColdFusion news from Macromedia, your technical articles. from SitePoint, and your digital security news from CNET, you might want to create a single Web page that allows you to see all the news at one time. In this article, we’re going to talk about how you can do this very quickly (and I mean very quickly!) and easily in ColdFusion MX.

例如,如果您从Macromedia获取ColdFusion新闻 ,请参阅技术文章 。 来自SitePoint的信息,以及来自CNET的数字安全新闻 ,您可能希望创建一个网页,使您可以一次查看所有新闻。 在本文中,我们将讨论如何在ColdFusion MX中轻松快速地做到这一点(我的意思是很快!)。

It’s important to note that, because RSS is an XML-based technology, you must use ColdFusion MX in order to follow along with the examples in this article. There are ways of using XML with older versions, but they are overly complicated and not always as stable as MX’s built-in functionality.

需要特别注意的是,由于RSS是一种基于XML的技术,因此您必须使用ColdFusion MX才能遵循本文中的示例。 有一些将XML与较早版本一起使用的方法,但是它们过于复杂,并且并不总是与MX的内置功能一样稳定。

什么是RSS? (What is RSS?)

First, let’s talk about what RSS is. I’m not going to give you the entire history here, but I will provide links to very important information that will help you to get started with the concept behind RSS. To sum it up very simple, RSS (Real Simple Syndication or RDF Site Summary) is a clean, straightforward method of sharing content between Websites. RSS can work for you in several ways:

首先,让我们谈谈RSS是什么。 我不会在这里提供整个历史记录,但是我将提供指向非常重要的信息的链接,这些信息将帮助您入门RSS背后的概念。 总而言之,RSS(Real Simple Syndication或RDF网站摘要)是一种在网站之间共享内容的干净,直接的方法。 RSS可以通过几种方式为您工作:

  1. RSS can allow you to keep news items updated on your site without having to manually change or update the news. If you run a health-related Website, you might want to put health-related news on the site. In this case, you could use MedicineNet’s RSS feed to keep the news updated.

    RSS可以让您保持站点上新闻的更新,而不必手动更改或更新新闻。 如果您运行与健康相关的网站,则可能要在网站上发布与健康相关的新闻。 在这种情况下,您可以使用MedicineNet的RSS feed来更新新闻。

  2. You can create your own RSS feed that will allow visitors to your Website to syndicate your content. This can increase your visibility among sites of a similar genre, increase traffic to your site, and help you to build a recognizable name.

    您可以创建自己的RSS供稿,以允许您网站的访问者联合您的内容。 这样可以提高您在类似类型的网站之间的知名度,增加网站访问量,并帮助您建立一个可识别的名称。
  3. You can use RSS to create your own personal home page for keeping track of news items you usually have to visit several sites to get. This also gives you the ability to manipulate the way the information is presented and how often it’s updated.

    您可以使用RSS创建自己的个人主页,以跟踪通常需要访问多个站点才能获得的新闻。 这也使您能够操纵信息的显示方式和更新频率。

The possibilities for RSS feeds are unlimited. Before we move too much further, though, I’m going to list some resources you may want to read before delving into the rest of the article.

RSS提要的可能性是无限的。 不过,在进行进一步的介绍之前,我将列出在研究本文其余部分之前您可能需要阅读的一些资源。

检索和缓存RSS源 (Retrieving and Caching RSS Feeds)

In my article on HTTP, I discussed how to retrieve an RSS feed using HTTP. To recap that, we’ll go ahead and talk about how to retrieve the RSS feed and cache it for later use. First, we’re going to use a very simple HTTP call to retrieve the feed. We’ll work with the RSS feed for my blog here at SitePoint as our example. To retrieve the blog feed, we’ll use an HTTP call that looks like this:

有关HTTP的文章中 ,我讨论了如何使用HTTP检索RSS feed。 概括地说,我们将继续讨论如何检索RSS feed并将其缓存以备后用。 首先,我们将使用一个非常简单的HTTP调用来检索提要。 作为示例,我们将在SitePoint上为我的博客使用RSS feed 。 要检索博客供稿,我们将使用如下所示的HTTP调用:

<cfhttp url="https://www.sitepoint.com/blog.rdf?blogid=7" method="get" timeout="5" path="C:temp" file="dave_blog.rdf" />

This tag retrieves the RSS feed from SitePoint and stores it in a file called "dave_blog.rdf". It’s important to remember that, because we specify the path and file, there will be no FileContent variable in the cfhttp struct. This means we’re going to have to read the file into a variable:

此标记从SitePoint检索RSS提要,并将其存储在名为"dave_blog.rdf"的文件中。 重要的是要记住这一点,因为我们指定的路径和文件,就不会有FileContent在变量cfhttp结构。 这意味着我们将不得不将文件读入一个变量:

<cffile action="read" file="C:tempdave_blog.rdf" variable="FileContent">

Now we have a variable called FileContent that contains the RDF document for the feed. We’re eventually going to parse this XML document and output the information. But, before we do so, let’s discuss caching.

现在,我们有了一个名为FileContent的变量,其中包含供稿的RDF文档。 我们最终将解析此XML文档并输出信息。 但是,在我们开始之前,让我们讨论一下缓存。

If you’re going to put my blog feed on your site and you have 1,000 visitors a day then, chances are, you’re going to have to make an HTTP call to SitePoint 1,000 times a day. To prevent overloading their server, and to speed up the display of this feed on your own site, you should definitely cache the file on your server. This is why we’ve specified a path and file name to which we’re going to save the feed. I’m thinking that I want to update the feed every 24 hours so that I’ll be sure to get fresh content, but I won’t hit SitePoint very often at all. Here’s the complete code (cache.cfm) I’ll use to fetch the feed as needed and cache it to a file:

如果您要将我的博客供稿放到您的网站上,并且每天有1,000位访问者,那么您很有可能每天必须对SitePoint进行一次HTTP调用1000次。 为防止其服务器超载,并加快此提要在您自己的站点上的显示,您绝对应该将文件缓存在服务器上。 这就是为什么我们指定了将提要保存到的路径和文件名的原因。 我认为我想每24小时更新一次Feed,以便确保获得新鲜的内容,但是我根本不会经常访问SitePoint。 这是完整的代码( cache.cfm ),我将根据需要使用此代码来提取供稿并将其缓存到文件中:

<cfset SavePath = ExpandPath("./cache.cfm")>
<cfset SavePath = Replace(SavePath, "cache.cfm", "blog.rdf")>
<cfdirectory action="list" directory="#GetDirectoryFromPath(SavePath)#" name="blogRdf" filter="blog.rdf">
<cfif not blogRdf.RecordCount eq 0 and DateCompare(blogRdf.DateLastModified, DateAdd("h", -24, Now())) eq -1>
 <cffile action="delete" file="#SavePath#">
</cfif>
<cfif FileExists(SavePath) eq 0>
 <cfhttp url="https://www.sitepoint.com/blog.rdf?blogid=7" method="get" timeout="10" path="#GetDirectoryFromPath(SavePath)#" file="#GetFileFromPath(SavePath)#"></cfhttp>
</cfif>  
<cffile action="read" file="#SavePath#" variable="FileContent">

This is a pretty big chunk of code, so I’ll break it down line by line for you. It’s really very simple.

这是一大段代码,因此我将为您逐行将其分解。 这真的非常简单。

  1. I create the SavePath variable, which is simply the path to the current template file.

    我创建了SavePath变量,它只是当前模板文件的路径。

  2. I replace the name of my current template with the name of the file in which I want to store the feed, "blog.rdf".

    我将当前模板的名称替换为要在其中存储供稿的文件名称"blog.rdf"

  3. I get a directory listing of the directory that I stored the feed in.

    我得到了存储提要的目录的目录清单。
  4. If the file exists in the directory and it is older than 24 hours, I delete it.

    如果该文件存在于目录中并且早于24小时,则将其删除。
  5. If the file does not exist (remember, it’s deleted every 24 hours), I do an HTTP call to retrieve it and I store it in the file.

    如果该文件不存在(请记住,它每24小时就会删除一次),那么我会执行一次HTTP调用来检索该文件并将其存储在文件中。
  6. I read in the file and store it as "FileContent" for parsing later.

    我读入文件并将其存储为"FileContent"以供以后解析。

And that is pretty much all that has to be done for our retrieval and caching mechanism. That was easy, wasn’t it? The rest is easy, too.

这几乎是我们的检索和缓存机制所要做的所有事情。 那很容易,不是吗? 其余的也很容易。

解析和显示RSS Feed (Parsing and Displaying the RSS Feed)

If you look at the raw XML output of the RSS feed, you’ll be able to see that its structure contains a root node called "rdf:RDF". We then have a "channel" node with a title, link, description, and image. We then have an "items" node that contains an array of "li" nodes ("rdf:li"). We have an "image" node that contains information about the SitePoint logo, including the title of the image, the link to the blog (to go into the href of your anchor tag), and the URL of the image itself. Then, we have an array of "item" nodes, each or which contains a title, a direct link to the blog post, a description, and the date. Notice that the date is in the "dc" namespace and it’s not a format that ColdFusion is very friendly with. I’ll show you a brute force method of displaying the date as well.

如果查看RSS feed的原始XML输出,您将能够看到其结构包含一个名为“ rdf:RDF”的根节点。 然后,我们有一个带有标题,链接,描述和图像的“通道”节点。 然后,我们有一个“ items”节点,其中包含“ li”个节点(“ rdf:li”)的数组。 我们有一个“图像”节点,其中包含有关SitePoint徽标的信息,包括图像的标题,博客链接(进入锚标签的href)和图像本身的URL。 然后,我们有一个“项目”节点数组,每个节点或每个节点都包含一个标题,指向博客文章的直接链接,描述和日期。 请注意,日期位于“ dc”命名空间中,而不是ColdFusion非常友好的格式。 我还将向您展示一种显示日期的蛮力方法。

The first thing you’ll have to do is decide what you want to display and how you want to display it. I’m going to have a single page that displays the title and description of the blog as well as a link to it and the SitePoint logo. I’ll then display each blog entry — the title will be linked to the entry on SitePoint’s Website and, beneath it, the description and date will appear. Here’s the code I use to do this:

您要做的第一件事是确定要显示的内容以及显示方式。 我将有一个页面显示博客的标题和描述,以及指向该博客和SitePoint徽标的链接。 然后,我将显示每个博客条目-标题将链接到SitePoint网站上的条目,并在其下方显示描述和日期。 这是我用来执行此操作的代码:

<cfset xmlDoc = XMLParse(filecontent)>
<cfoutput>
 <img src="#xmlDoc.rdf.image.url.xmlText#" align="left">
 Source Title: #xmlDoc.rdf.channel.title.xmlText#<br>
 Link: <a href="#xmlDoc.rdf.channel.link.xmlText#">#xmlDoc.rdf.channel.link.xmlText#</a><br />
 Description: #xmlDoc.rdf.channel.description.xmlText#
 <hr />
 <cfloop from="1" to="#ArrayLen(xmlDoc.rdf.item)#" index="i">
   <cfset dt = Replace(xmlDoc.rdf.item[i].date.xmlText, "T", " ")>
   <cfset dt = Replace(dt, "Z", " ")>
   <p>
   <a href="#xmlDoc.rdf.item[i].link.xmlText#">#xmlDoc.rdf.item[i].title.xmlText#</a><br />
   #xmlDoc.rdf.item[i].description.xmlText#<br />
   <em>(Posted on #DateFormat(dt, "mm/dd/yyyy")#)</em>
   </p>
 </cfloop>
</cfoutput>

I’ll walk you through the code we’re using to parse and display the feed. First, we use the XMLParse(string) function to parse the document. Note that this is also a validating parser, so it will choke if you pass it invalid XML. When you use this function, it creates an XML Document object. Essentially, this is a series of structures and arrays all nested inside one another.

我将向您介绍我们用于解析和显示提要的代码。 首先,我们使用XMLParse(string)函数来解析文档。 请注意,这也是一个验证解析器,因此如果传递无效的XML,它会阻塞。 使用此功能时,它将创建一个XML Document对象。 本质上,这是一系列相互嵌套的结构和数组。

Next, we’ll open our cfoutput and we’ll begin to display information. My image source is going to be the value contained in xmlDoc.rdf.image.url.xmlText. If you look carefully, you’ll see that you can access values simply by "walking" down the document tree to the value you need. We start with the xmlDoc variable, then go to the root node of rdf. Our image information is in the image node, under the url node. The value is contained in the xmlText element of the url node.

接下来,我们将打开cfoutput并开始显示信息。 我的图像源将是xmlDoc.rdf.image.url.xmlText包含的值。 如果仔细看,您会发现只需将文档树“遍历”到所需的值即可访问值。 我们从xmlDoc变量开始,然后转到rdf的根节点。 我们的图像信息位于url节点下的image节点中。 该值包含在url节点的xmlText元素中。

As a side point, I should explain how to access attributes in an XML document. Let’s access the "about" attribute of the image node. Here’s the path to it: xmlDoc.rdf.image.xmlAttributes.about. To access an attribute, refer to the xmlAttributes struct that is attached to the node in which the attribute is stored; then, simply access the attribute name.

附带说明一下,我应该解释如何访问XML文档中的属性。 让我们访问图像节点的“关于”属性。 这是它的路径: xmlDoc.rdf.image.xmlAttributes.about 。 要访问属性,请参考附加到存储该属性的节点的xmlAttributes结构。 然后,只需访问属性名称。

You can apply the above logic to display the title, description, and link to the blog. Then, we want to display the items. To do this, I’m simply going to loop from 1 to the length of the array found at xmlDoc.rdf.item. As I’m looping over it, I’ll access the item[i].variableName variable to display the desired information. Note that you must fully qualify the path to the value, otherwise ColdFusion won’t know where to look for the variable.

您可以应用以上逻辑来显示标题,描述和指向博客的链接。 然后,我们要显示项目。 为此,我将简单地从1循环到xmlDoc.rdf.item找到的数组的长度。 在遍历它时,我将访问item[i].variableName变量以显示所需的信息。 请注意,您必须完全限定值的路径,否则ColdFusion将不知道在哪里寻找变量。

That was pretty easy, too, right? We now have a total of about 37 lines of code that we’re using to display the blog feed itself. Your assignment is to modify the code to make it prettier, display only x rows on one page, and allow for premature refreshing of the cache (before the 24 hours is up).

那也很容易,对吧? 现在,我们总共使用了约37行代码来显示博客供稿本身。 您的任务是修改代码,使其更漂亮,在一页上仅显示x行,并允许过早刷新缓存(在24小时之前)。

Before I move on, let me talk about the date, though. This date is not presented in a format that ColdFusion understands. Therefore, our script crashes pretty hard when it tries to format it using the DateFormat(date, mask) function. To fix this, you simply need to replace the "T" and "Z" with spaces, as you see I’ve done above. Then, you can display it however you like.

不过,在继续之前,让我谈谈日期。 该日期未以ColdFusion可以理解的格式显示。 因此,当脚本尝试使用DateFormat(date, mask)函数设置其格式时,其崩溃非常困难。 要解决此问题,您只需要用空格替换“ T”和“ Z”即可,就像您在上文中看到的那样。 然后,您可以根据需要显示它。

为您的站点创建RSS feed (Creating an RSS Feed for Your Site)

If you run a content site and you’d like to syndicate your content to other sites, the easiest way to do so is to create an RSS feed. I’m going to use RSS version 0.91 to create a content feed of my own. You can use RSS 1.0, as SitePoint does, if you’d prefer. Just make a few minor modifications so that the document is compliant with the standard.

如果您运行一个内容站点,并且希望将您的内容联合到其他站点,那么最简单的方法是创建一个RSS feed。 我将使用RSS 0.91版创建自己的内容供稿。 如果愿意,可以像SitePoint一样使用RSS 1.0。 只需稍作修改,以使文档符合标准

My approach to delivering the content feed is that I’ll have a scheduled task that runs every 24 hours. (If you have a site that is very dynamic and constantly changing, you may want to decrease this interval.) This scheduled task will create the RSS feed and write it to a file on the server, which can then easily be accessed by all. It’s a very simple process and the code looks like this:

我提供内容供稿的方法是,我将有一个计划的任务,每24小时运行一次。 (如果您的站点非常动态且不断变化,则可能需要缩短此间隔。)此计划任务将创建RSS feed,并将其写入服务器上的文件,然后所有人都可以轻松访问。 这是一个非常简单的过程,代码如下所示:

<cfquery name="GetArticles" datasource="#dsn#">
  SELECT TOP 5 Title, Description, ID
  FROM Articles
  ORDER BY ID DESC
</cfquery>
<cfxml variable="rssFeed">
  <rss version="0.91">
     <channel>
        <title>My Content Site – Recent Articles</title>
        <link>http://www.example.com/</link>
        <description>The best place to get content.</description>
        <image>
           <title>Example.com</title>
           <url>http://www.example.com/logo.gif</url>
           <link>http://www.example.com/</link>
        </image>
        <cfoutput query="GetArticles">
           <cfset Desc = REReplaceNoCase(Description, "<[^>]*>", "", "ALL")>
           <cfset Desc = Replace(Desc, "&nbsp;", " ", "ALL")>
           <item>
              <title>#Title#</title>
              <link>http://www.index.com/article/index.cfm?ArticleID=#ID#</link>
              <description>#Desc#</description>
           </item>
        </cfoutput>
     </channel>
  </rss>
</cfxml>
<cffile action="write" file="#ExpandPath("./myArticles.xml")#" output="#ToString(rssFeed)#">

Don’t be concerned with the query or the database structure at this point. Just know that the information I’m getting from the database is the title, description, and link (in this case, the ID for the link) to be used in the RSS feed.

此时,不必担心查询或数据库结构。 只需知道我从数据库中获得的信息就是RSS提要中使用的标题,描述和链接(在本例中为链接的ID)即可。

The next notable, and the most important, thing in this file is the use of cfxml. This tag is being used to create an XML document that will later be converted to a string and written to a file. Notice that we specify the variable attribute, which will be the ColdFusion variable that holds the XML document. The variable name is not the top level node! Don’t let that fool you. Next, we simply insert the RSS nodes with which we want to provide our users, including the title, link, URL, and image information. We then loop over the query and add each item node with the title, link, and description.

该文件中下一个值得注意的也是最重要的事情是cfxml的使用。 此标记用于创建XML文档,该XML文档稍后将转换为字符串并写入文件。 注意,我们指定了variable属性,该属性将是保存XML文档的ColdFusion变量。 变量名称不是顶级节点! 不要让那个愚弄你。 接下来,我们仅插入要向其提供用户的RSS节点,包括标题,链接,URL和图像信息。 然后,我们遍历查询并添加带有标题,链接和描述的每个item节点。

Finally, we use the ToString(string) function to convert the XML document created in cfxml into a string that can be written to a file. Run the template and then point your browser at the "./myArticles.xml" file in the same directory as the template. (You can move this anywhere on your server as long as it is accessible via the Web. Otherwise, it does no good.)

最后,我们使用ToString(string)函数将在cfxml创建的XML文档转换为可以写入文件的字符串。 运行模板,然后将浏览器指向与模板相同目录中的“ ./myArticles.xml”文件。 (只要可以通过Web访问它,就可以将其移动到服务器上的任何位置。否则,它就没有用。)

For another way to create an RSS feed, you may be interested in this tutorial by Pablo Varando. It’s a different approach that works just as well. Note that it’s been tested on BlueDragon as well. My example has not, but the BlueDragon server supports all the tags and methods I used, so, technically, it should work fine.

对于创建RSS feed的另一种方法,您可能对Pablo Varando的本教程感兴趣。 这是一种同样有效的不同方法。 请注意,它也在BlueDragon上进行了测试。 我的示例没有,但是BlueDragon服务器支持我使用的所有标记和方法,因此,从技术上讲,它应该可以正常工作。

结语 (Wrap Up)

We’ve now seen how to retrieve and display an RSS/RDF feed. The example we used was RSS 1.0, but the principles are the same for using RSS 0.91. We’ve also seen how to create our own RSS feed for our users to access so they can display our content on their sites.

现在,我们已经看到了如何检索和显示RSS / RDF提要。 我们使用的示例是RSS 1.0,但是原理与使用RSS 0.91相同。 我们还看到了如何创建供用户访问的RSS供稿,以便他们可以在其网站上显示我们的内容。

Now, I’m going to introduce something new to my articles: assignments. Here are a few assignments that you can complete to enhance your skills in this area. If you need help with these assignments, please feel free to discuss them below or on the SitePoint Forums. I’m generally available to help and, on the forums, there are many very skilled ColdFusion developers, who offer their advice and viewpoints readily.

现在,我将为我的文章介绍一些新内容:作业。 您可以完成一些作业,以增强自己在该领域的技能。 如果您需要有关这些作业的帮助,请随时在下面或在SitePoint论坛上进行讨论。 我通常可以为您提供帮助,在论坛上,有许多非常熟练的ColdFusion开发人员,他们随时可以提供建议和观点。

  1. Modify the RSS reader to use RSS 0.91.

    修改RSS阅读器以使用RSS 0.91。
  2. Create a custom tag that accepts a URL for an RSS feed and then retrieves it, caches it, parses it, and displays it. Set it up to accept either RSS 0.91 or RSS 1.0.

    创建一个自定义标签,该标签接受RSS feed的URL,然后对其进行检索,缓存,解析并显示。 将其设置为接受RSS 0.91或RSS 1.0。
  3. Modify the RSS creation script above to create an RSS 1.0 feed.

    修改上面的RSS创建脚本以创建RSS 1.0 feed。
  4. Create an RSS feed for your site (if you have a content or news based site).

    为您的站点创建一个RSS feed(如果您有基于内容或新闻的站点)。

There are many other ways that RSS can be used on the Web. In fact, the more you think about it, the more ideas you’ll find, I’m sure. Feel free to share those ideas with us using the discussion box below. I hope to see many ColdFusion developers using RSS feeds in the near future. Until next time, keep it Cold!

可以在Web上使用RSS的其他方式很多。 实际上,我敢肯定,您考虑得越多,就会找到更多的想法。 欢迎使用下面的讨论框与我们分享这些想法。 我希望在不久的将来看到许多ColdFusion开发人员使用RSS feed。 直到下一次,保持寒冷!

翻译自: https://www.sitepoint.com/content-syndication-coldfusion/

coldfusion

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值