Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Set Up a Simple Syndication Feed Using RSS : Page 3

You can set up outbound syndication for your Web site and expose your content to the world in one afternoon using an XML technology called RSS. For anyone who ever wanted to increase audience and traffic while maintaining content and presentation control, RSS is the answer. Find out how to do it, step by step.


advertisement
Creating the File
To create the file, you first need to query the database to grab the content descriptions you want to syndicate. The SQL code below is a quick, simple method of doing this. You will need to modify it to fit your situation. The <cfquery> tags are specific to ColdFusion.

The code in this section of the article is taken from the complete sample code, which is available for download here.

<cfset NumberOfFeedItems = 10> <cfquery name="getLatestPublishedArticles" datasource="yourDB"> SELECT TOP #numberOfFeedItems# * FROM yourContent ORDER BY PublishedDate DESC </cfquery>

Next, set a date variable.


<cfset theDatetime = "#dateformat(now(), "ddd, dd mmm yyyy")# #timeformat(now(), "HH:mm:ss")# PST">

Now, save the output to a variable. In my ColdFusion version I saved the output to a variable called "theXML." It is a string that will contain all my XML.

<cfsetting enablecfoutputonly="yes"> <cfsavecontent variable="theXML">

Now write out the XML text. Here is the top portion of the file:

<cfoutput> <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- RSS generated by DevX on #theDatetime# --> <rss version="2.0"> <channel> <title>DevX Featured Content</title> <link>http://www.devx.com</link> <description>Latest DevX Content</description> <language>en-us</language> <copyright>Copyright 2002 DevX</copyright> <docs>http://backend.userland.com/rss</docs> <lastBuildDate>#theDatetime#</lastBuildDate> <image> <title>DevX</title> <url>http://www.devx.com/assets/devx/3182.gif</url> <link>http://www.DevX.com</link> </image> </cfoutput>

The next step is to start adding content items that you want to syndicate. In ColdFusion, I did this with a simple loop. In other words, I loop over the values returned from the first query I did:

<cfloop from="1" to = "#numberOfFeedItems#" index="ctr">

Now set up variables with the data for each item. This is also a good place to "massage" the data so that you can avoid having any illegal characters or tags—you must have properly formatted XML. I used the "Replace" function to replace "<" signs with "& l t ;." This gets around the problem of embedding HTML in your feed. If you might have HTML tags embedded in your XML, then the XML will not validate correctly unless you do this kind of replacement.

Also, if you have URLs with URL parameters embedded in your fields, you must replace the "&" signs with "& a m p ;" so the XML will validate correctly. For example, in the URL http://www.aWebSite.com/index.htm?value1=one&value2=two, the "&" sign in the string will cause trouble.

<cfscript> title = replace(getContent.title[ctr], "<", "<", "ALL"); description = replace(getContent.abstract[ctr], "<", "<", "ALL"); description = replace(description, "&", "&", "ALL"); date = dateformat(getContent.dateFirstPublished[ctr], "ddd, dd mmm yyyy"); time = timeformat(getContent.dateFirstPublished[ctr], "HH:mm:ss") & " PST"; author = replace(getContent.author[ctr], "<", "<", "ALL"); pubDate = date & " " & time; </cfscript>

Then output the fields for each item:

<cfoutput> <item> <title>#title#</title> <description>#description#</description> <link>http://www.devx.com/content/id/#getContent.content_id[ctr]#</link> <author>#authors#</author> <pubDate>#pubDate#</pubDate> </item> </cfoutput>

Now close the loop, close the channel and rss tags, and close the cfsavecontent tag.

</cfloop> <cfoutput> </channel> </rss> </cfoutput> </cfsavecontent>

The XML is stored in "theXml" variable, and you can write it to a publicly accessible Web directory using this line:

<cffile action="write" file="c:\devx\syndication\outgoing\devxFeed.xml" output="#theXml#">

Finally, set the MIME type of the document so the browser knows the output is in XML. This is an optional step and simply allows you to see the XML you generate in a browser.

<cfcontent type="text/xml"> <cfoutput>#theXml#</cfoutput>



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap