Thursday, September 18, 2008

RSSfeed IN Coldfusion

Creating RSSfeeds IN Coldfusion:-
When you create a feed, you specify the feed contents in a name structure or in the combination of a query object and a properties structure. The cffeed tag generates the feed XML and saves in to the variable specified by the xmlVar attribute, the file specified by the outputFile attribute, or both.
To create an RSS 2.0 feed you must specify the following metadata fields in a name structure or in a properties structure. All other RSS2.0 metadata fields, and all item fields, are optional.
version (must be "rss_2.0")
The cffeed tag does not enforce any rules on the Atom feed structure that it creates. You are responsible for ensuring that the feed is valid.
In most cases, a database table uses column names that differ from the column names you must use to create the feed. Therefore, you must use the columnmap attribute to map the input query column names to the required column names. The attribute is a structure whose keys are the column names required by the cffeed tag and whose values are the corresponding input query columns. The following example creates a feed using the cfartgallery data source's orders table. It maps the orders table ORDERDATE column to the query publisheddate column, the ADDRESS column to the content column, and so on. The sample code then displays the generated query XML to show the results.
<!--- Get the feed data as a query from the orders table. --->
<cfquery name="getOrders" datasource="cfartgallery">
SELECT * FROM orders

<!--- Map the orders column names to the feed query column names. --->
<cfset columnMapStruct = StructNew()>
<cfset columnMapStruct.publisheddate = "ORDERDATE">
<cfset columnMapStruct.content = "ADDRESS">
<cfset columnMapStruct.title = "CUSTOMERFIRSTNAME">
<cfset columnMapStruct.rsslink = "ORDERID">

<!--- Set the feed metadata. --->
<cfset meta.title = "Art Orders">
<cfset = "http://feedlink">
<cfset meta.description = "Orders at the art gallery">
<cfset meta.version = "rss_2.0">

<!--- Create the feed. --->
<cffeed action="create"

<cfdump var="#XMLParse(rssXML)#">

Reading feeds
The cffeed tag does not validate the feeds that it reads. It can read invalid or loosely formatted feeds, but ignores some or all of the invalid content. For example, if you put more than one rights element in the Atom feed (which invalidates the feed), the cffeed tag ignores the elements after the first one, and doesn't generate an error.
Dates and times in feeds that are being read must be in W3C or RFC 822 format. ColdFusion can also read iTunes extension dates in the format normally used by the iTunes music store.
The following example creates an RSS feed. You must enter fields for the feed title, link, and description elements. You must also enter title, link, and description fields for one item. A second item is optional. The application saves the feed in a createRSSOutput.xml file in the feedTest subdirectory of the directory that contains the CFML page.
<!--- Generate the feed when the user submits a filled in form. --->
<cfif isDefined("Form.Submit")>

// Create the feed data structure and add the metadata.
myStruct = StructNew(); =;
myStruct.title = form.title;
mystruct.description = form.description;
mystruct.pubDate = Now();
mystruct.version = "rss_2.0";

/* Add the feed items. A more sophisticated application would use dynamic variables
and support varying numbers of items. */
myStruct.item = ArrayNew(1);
myStruct.item[1] = StructNew();
myStruct.item[1].description = StructNew();
myStruct.item[1].description.value = form.item1text;
myStruct.item[1].link = form.item1link;
myStruct.item[1].pubDate = Now();
myStruct.item[1].title = form.item1title;
myStruct.item[2] = StructNew();
myStruct.item[2].description = StructNew();
myStruct.item[2].description.value = form.item2text;
myStruct.item[2].link = form.item2link;
myStruct.item[2].pubDate = Now();
myStruct.item[2].title = form.item2title;


<!--- Generate the feed and save it to a file and variable. --->
<cffeed action = "create"
name = "#myStruct#"
outputFile = "feedTest/createRSSOutput.xml"
overwrite = "yes"
xmlVar = "myXML">


<!--- The user input form. --->
<cfform format="xml" preservedata="yes" style="width:500" height="700">
<cfformitem type = "text"> Enter The Feed Metadata</cfformitem>
<cfinput type = "text" label = "title" name = "title"
style = "width:435" required = "yes"> <br />
<cfinput type = "text" label = "link" name = "link"
style = "width:435" required = "yes" validate = "url"> <br />
<cftextarea name = "description"
style = "width:435; height:70" required = "yes" />

<cfformitem type = "text"> Enter Item 1</cfformitem>
<cfinput type="text" label="title" name="item1title"
style="width:435" required="yes"> <br />
<cfinput type="text" label="link" name="item1link"
style="width:435" required="yes" validate="url"> <br />
<cftextarea name = "item1text"
style = "width:435; height:70" required = "yes" /> <br />

<cfformitem type = "text"> Enter Item 2</cfformitem>
<cfinput type = "text" label = "title" name = "item2title" style = "width:435"> <br />
<cfinput type = "text" label = "link" name = "item2link" style = "width:435"
validate = "url"> <br />
<cftextarea name = "item2text" style = "width:435; height:70" /> <br />

<cfinput type = "Submit" name = "submit" value = "Submit" >

The following application is a simple feed reader that handles RSS and Atom feeds. It displays the feed title; for each item or entry, it shows the title as a link, and shows the published date and the item or entry contents. To use this example to read the feed created by the first application, enter the URL for the file the application created, for example, http://localhost:8500/cffeed/feedTest/createRSSOutput.xml.
<!--- Process the feed data if the user submitted the form --->
<cfif isDefined("Form.Submit")>
<cffeed source = "#theURL#"
properties = "myProps"
query = "myQuery">

<!--- Display the feed output.
Use conditional logic for to handle different feed formats. --->
<cfoutput query = "myQuery">
<cfif myProps.version IS "atom_1.0">
<h3><a href = "#linkhref#">#title#</a></h3>
<p><b>Published:</b> #DateFormat(publisheddate)#</p>
<h3><a href = "#rsslink#">#title#</a></h3>
<p><b>Published:</b> #publisheddate#</p>

<!--- The form for specifying the feed URL or file --->
<cfform name = "SetFeed" preserveData = "yes">
Enter Feed URL:
<cfinput type = "text" size = "60" name = "theURL"><br><br>
<cfinput type = "Submit" name = "submit" value = "Submit">

No comments: