School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,479 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,755 people online right now. Registration is fast and FREE... Join Now!




Coldfusion and XML

 

Coldfusion and XML, How to response an xml file to the browser with CFML

midasxl

22 Apr, 2009 - 05:23 AM
Post #1

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 125



Thanked: 1 times
My Contributions
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...

CODE

<!---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!

User is offlineProfile CardPM
+Quote Post


xheartonfire43x

RE: Coldfusion And XML

22 Apr, 2009 - 06:36 AM
Post #2

D.I.C Regular
***

Joined: 22 Dec, 2008
Posts: 260



Thanked: 2 times
My Contributions
QUOTE(midasxl @ 22 Apr, 2009 - 05: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...


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>

CODE

<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.

User is offlineProfile CardPM
+Quote Post

sansclue

RE: Coldfusion And XML

22 Apr, 2009 - 06:46 AM
Post #3

D.I.C Regular
***

Joined: 21 Nov, 2007
Posts: 316



Thanked: 28 times
My Contributions
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>

User is offlineProfile CardPM
+Quote Post

midasxl

RE: Coldfusion And XML

22 Apr, 2009 - 07:40 AM
Post #4

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 125



Thanked: 1 times
My Contributions
Thank you friends for your suggestions. I tried three things...


cfoutput; this worked as I had hoped
CODE

<cfoutput>#xmlobject#</cfoutput>


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

<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
CODE

<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!!

This post has been edited by midasxl: 22 Apr, 2009 - 07:50 AM


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

sansclue

RE: Coldfusion And XML

22 Apr, 2009 - 10:27 AM
Post #5

D.I.C Regular
***

Joined: 21 Nov, 2007
Posts: 316



Thanked: 28 times
My Contributions
QUOTE(midasxl @ 22 Apr, 2009 - 07:40 AM) *

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.adobe.com/coldfusion/8/htm.../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);">

User is offlineProfile CardPM
+Quote Post

midasxl

RE: Coldfusion And XML

27 Apr, 2009 - 07:45 AM
Post #6

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 125



Thanked: 1 times
My Contributions
Thanks for all your help on this one. Here's what I ended up going with...

CODE

<!---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!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:42AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month