Coldfusion and XML

How to response an xml file to the browser with CFML

Page 1 of 1

5 Replies - 10173 Views - Last Post: 27 April 2009 - 08:45 AM Rate Topic: -----

#1 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 215
  • Joined: 03-December 08

Coldfusion and XML

Post icon  Posted 22 April 2009 - 06:23 AM

Hello again! Thanks for reading.

I have a snippet of code that successfully queries a database and creates an XML file from the results. Is there a way to NOT write the xml file but just have the code display the xml in the browser. I'm picturing it creating the xml each time the page is called and just do a response to the browser. Not sure if this is possible.

I'm trying to avoid actually having a physical copy of the .xml file anywhere someone might be able to get to it. It holds sensitive data.

Here is what I have...

<!---Query the database--->
<cfquery name="feed" datasource="public_www">
SELECT *
FROM rss_current_news
WHERE active_flag = 1
</cfquery>

<!---***********************************begin .xml generation**********************************************--->

<!---Remove white space from generated content in the tag body--->
<cfprocessingdirective suppresswhitespace="yes">
<!---Create a CF XML document object that contains the markup--->
<cfxml variable="xmlobject" casesensitive="yes">

<rss version="2.0">
<channel>
<title>NCDOC RSS Feed</title>
<link>rss/feed/rssFeed.xml</link>
<description>RSS Feed for Current NCDOC News </description>
<lastbuilddate>Date and Time of Last Change</lastbuilddate>
<language>en-us</language>
	<cfoutput>
			   
	  <cfloop query="feed">
	  
			  <item>
				<title>#XmlFormat(feed.title)#</title>
				<description>#XmlFormat(feed.caption)#</description>
				<date>#XmlFormat(feed.mod_datetime)#</date>
				<link>#XmlFormat(feed.link)#</link>
			</item>
	  
	   </cfloop>
	   
	 </cfoutput>
</channel>
</rss>

</cfxml>
</cfprocessingdirective>

<!--- Convert the XML document object to a string--->
<cfset xmlString = ToString(xmlobject)>

<cffile action="write" file="/usr/local/apache2/htdocs/www/rss/feed/rssTest.xml" mode="777" output="#xmlString#">


<!---***********************************end .xml generation**********************************************--->



The <cffile action="write"> would ideally be something like <appropriatetag action="display xml in the browser">
Thanks for any info you may be able to provide!

Cheers!

Is This A Good Question/Topic? 0
  • +

Replies To: Coldfusion and XML

#2 xheartonfire43x   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Coldfusion and XML

Posted 22 April 2009 - 07:36 AM

View Postmidasxl, on 22 Apr, 2009 - 05:23 AM, said:

Hello again! Thanks for reading.

I have a snippet of code that successfully queries a database and creates an XML file from the results. Is there a way to NOT write the xml file but just have the code display the xml in the browser. I'm picturing it creating the xml each time the page is called and just do a response to the browser. Not sure if this is possible.

I'm trying to avoid actually having a physical copy of the .xml file anywhere someone might be able to get to it. It holds sensitive data.

Here is what I have...


The <cffile action="write"> would ideally be something like <appropriatetag action="display xml in the browser">
Thanks for any info you may be able to provide!

Cheers!


Yeah just do a <cfoutput>

<cfoutput>#xmlstring#</cfoutput>



That will return it just as a string without the XML tags though. I would say look on the livedocs for a function.
Was This Post Helpful? 0
  • +
  • -

#3 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Coldfusion and XML

Posted 22 April 2009 - 07:46 AM

You don't have to create a physical file. Just output the variable, with the right headers. The headers will tell the browser that the content is xml and whether to download or display it. I don't know the exact syntax or mime types offhand, but it will probably involve either two cfheaders or a combination of cfheader and cfcontent.

<cfheader ..>
<cfheader ..>
<cfoutput>#yourXMLContentVariable#</output>
Was This Post Helpful? 0
  • +
  • -

#4 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 215
  • Joined: 03-December 08

Re: Coldfusion and XML

Posted 22 April 2009 - 08:40 AM

Thank you friends for your suggestions. I tried three things...


cfoutput; this worked as I had hoped
<cfoutput>#xmlobject#</cfoutput>



cfcontent did not, because I assume it needs a path to an already written file, which I want to avoid
<cfcontent type="application/html+xml"
	 file="path to file"
	 deletefile="no">



cfheader returned the xml data but only a portion of it, not the entire construct. Very strange
<cfheader name="rss" value="#xmlobject#">



I substituted #xmlobject# with #xmlString# and got the same results all the way around. In the end the cfoutput was the best option.

So now when I run this action page, I get a cfoutput result which shows me my xml data in the browser. It looks like the pic I have attached. What I would like to do now is have a user copy and paste the url into their rss feed reader of choice. But the page is a .cfm page, not .rss or .xml

Can I somehow change the file extension of the page before it ouputs to the browser? This sounds like a long shot to me, but I was just wondering. I suspect not. You could probably easily change the file extension on an already written .xml or .cfm page, but I doubt it in this manner. Just thought I would check anyways. Thanks again!!

Attached image(s)

  • Attached Image

This post has been edited by midasxl: 22 April 2009 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Coldfusion and XML

Posted 22 April 2009 - 11:27 AM

View Postmidasxl, on 22 Apr, 2009 - 07:40 AM, said:

cfcontent did not, because I assume it needs a path to an already written file, which I want to avoid


Never assume ;-) .. when in doubt check the documentation. "File" is optional.
http://livedocs.adob.../Tags_c_11.html

Most examples you will see for downloading excel/csv should work for you. You just need to change the file name, extension and mime type to that for XML instead of excel.

See the "Usage" section for descriptions:
<cfheader name="Content-Disposition" value="attachment; filename=filename.ext">
.. OR ...
<cfheader name="Content-Disposition" value="inline; filename=name.ext">
<cfcontent type="(mime type for xml);">
Was This Post Helpful? 0
  • +
  • -

#6 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 215
  • Joined: 03-December 08

Re: Coldfusion and XML

Posted 27 April 2009 - 08:45 AM

Thanks for all your help on this one. Here's what I ended up going with...

<!---Query the database--->
<cfquery name="feed" datasource="public_www">
SELECT *
FROM rss_current_news
WHERE active_flag = 1
</cfquery>

<!---***********************************begin .xml generation**********************************************--->

<!---Remove white space from generated content in the tag body--->
<cfprocessingdirective suppresswhitespace="yes"><!---Prevents ColdFusion from putting white space characters in front of the XML declaration--->
<!---Create a CF XML document object that contains the markup--->
<cfxml variable="xmlobject" casesensitive="yes">

<rss version="2.0">
<channel>
<title>NCDOC RSS Feed</title>
<link>rss/feed/rssFeed.xml</link>
<description>RSS Feed for Current NCDOC News </description>
<lastbuilddate>Date and Time of Last Change</lastbuilddate>
<language>en-us</language>
	<cfoutput>
			   
	  <cfloop query="feed">
	  
			  <item>
				<title>#XmlFormat(feed.title)#</title>
				<description>#XmlFormat(feed.caption)#</description>
				<date>#XmlFormat(feed.mod_datetime)#</date>
				<link>#XmlFormat(feed.link)#</link>
			</item>
	  
	   </cfloop>
	   
	 </cfoutput>
</channel>
</rss>

</cfxml>
</cfprocessingdirective>

<cfoutput>#xmlobject#</cfoutput>

<!---***********************************end .xml generation**********************************************--->



This basically builds the xml format from the database query, and then outputs to the browser. In my old code I had it doing a write action and saving an .xml file, but I didn't want there to be an actual file out there for anyone to possibly grab. I named this page index.cfm and then just point to the folder it lives in so it will run automatically. Works well; hope other may find this helpful. Until next time!

Cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1