Controlling Whitespace in ColdFusion

With browsers being as generous as they are about whitespace, ColdFusion, like every other scripting language I have used, doesn’t seem to make much of an effort to keep it to a minimum. Typically, whitespace is not a concern since browsers handle it so well, however if you are trying to generate an XML document, it can be a big problem. That’s why we have tags like cfsilent, cfprocessingdirective, and cfsetting, and that’s why we have the ability to enable whitespace management in the ColdFusion administrator. However, CFMX for J2EE doesn’t seem to come with the whitespace management option, and sometimes no matter what combination of whitespace management tags I use, I simply cannot prevent the server from writing out a few carriage returns at the top of the document (which makes for invalid XML). I finally discovered the ultimate solution.

The best way I have found to prevent whitespace is to manage the buffer and the output stream yourself. When a page is being executed, generated content is being written to a buffer, and that buffer gets flushed to the output stream which represents the connection between your server and a browser. You can use the cfflush tag to flush that buffer if you need data to reach the client faster (for instance, if you want to give them feedback about a process which might take several seconds to complete), and you can also clear that buffer as well, which means you can get rid of unwanted whitespace! The only way I have found to do it is to resort to Java. The following does the trick:


The most bullet proof way I have found for generating valid XML in ColdFusion is to use a combination of the cfsavecontent tag and the Java code above, like this:

<cfset someVar = "Whitespace can be a pain."/><cfsavecontent variable="responseXml"><?xml version="1.0"?><root><element><cfoutput>#someVar#</cfoutput></element></root></cfsavecontent><cfscript>getPageContext().getOut().clearBuffer();writeOutput(responseXml);</cfscript>

Feel free to share your whitespace techniques here. I’m interested in knowing how others solve this problem.

14 Responses to Controlling Whitespace in ColdFusion

  1. My handling of XML whitespace has a lot to do with the way JournURL’s code is structured. It uses my own little Fusebox variant, and all output is handled by a system of skins that are processed at the end of a request.That means there are very, very few instances of CFOUTPUT in the code, and they only show up in predictable places. So the obvious thing was to simply set enablecfoutputonly=”true” and forget about it. One of the smaller (but more immediately tangible) benefits of separating code from presentation, I suppose.

  2. Samuel Neff says:

    I always used when working with XML to ensure there was no whitespace. Does that not work in CFMX/J2EE?

  3. Dave Carabetta says:

    Just wanted to point out that you can, in fact, control whitespace management if CFMX for J2EE, though not through the CF Administrator. I came across this bug buring one of the beta periods, and the Macromedia engineers posted this workaround, which is also in the Updater 3 Release Notes:”Whitespace management is supported in ColdFusion MX for J2EE on JRun 4, but it is disbled by default and you cannot set it in the ColdFusion MX Administrator. To enable whitespace management, shut down ColdFusion MX and change the setting of the first boolean value in the cf_root/WEB-INF/cfusion/lib/neo-runtime.xml to true:A Boolean value of true enables whitespace management; a value of false disbles it. After you have edited and saved the file, restart ColdFusion MX.”Your way is definitely a great tip, but I just thought I’d point this out in the interest of providing all workarounds.

  4. Back in the days before java was nice to use with CF we used CFCONTENT with “reset” attribute set to true — this works especially well for XML because you can then set the content type to text/xml very easily.Other things worth mentioning (not saying your suggestion is not a good one):CFSETTING allows to suppress everything about text inside of CFOUTPUT — it can make code messy, but works well.CFPROCESSINGDIRECTIVE has the suppresswhitespace attribute.CFSILENT is a quick and dirty way to make sure chunks of code don’t fill up the output buffer with white space.

  5. I hate spammers says:

    Christian,Just wanted let you know that the two comment posts above (by Court and Rosanna) are spam comments that you might want to remove…

  6. I Hate CF says:

    “ColdFusion, like every other scripting language I have used, doesn’t seem to make much of an effort to keep it to a minimum”Uhhh, I don’t know what scripting languages you are talking about, but Perl and PHP do NOT have this problem at all.The only reason why people use ColdFusion is because it is less bloated than ASP and ASP .NET and is the only viable alternative on Windows based platforms.And if you are on MX, and are using Java, why use CF at all? Just move over to a struts/Velocity/Servlet framework and do away with the crap called ColdFusion.Heck, I’ll go one step better and say you all should move over to Linux and enjoy the power of PHP, Perl, and Java – all integrated nicely and concurrently with the most reliable server in the world…Apache!Well for that matter, you you MUST have ColdFusion, still move over to Linux and STILL have Apache installed and share the love!Sheesh. Just remember that ColdFusion itself is NOT a programming language (thank you for not saying that) but rather must rely on other technologies to work.

  7. Bob says:

    Hey, I Hate CF – You’re an idiot.ColdFusion may not be a programming language in your opinion but who gives a rats ass about what you think.I certainly don’t and I am sure no one around here cares either..Get lost chump.

  8. Bob’s posting on June 29th is SPAM 🙂

  9. Jon Alsbury says:

    Christian,the most effective (and easy to implement) technique for reducing whitespace in CFMX generated pages I have discovered so far is to set up a simple servlet filter to intercept the response in order to strip out whitespace before it is returned to the client. The filer I’ve been using for this is called Trim Filter and can be downloaded here: is easy; simply download trimflt.jar from the above URL, drop it into your ‘cfusionmx/lib’ directory. Add the following to ‘cfusionmx/wwwroot/WEB-INF/web.xml’:<filter><filter-name>trimFilter</filter-name><filter-class>com.cj.trim.trimFilter</filter-class></filter><filter-mapping><filter-name>trimFilter</filter-name><url-pattern>*.cfm</url-pattern></filter-mapping>remember to clear out your ‘cfclasses’ dir and restart the cfmx process. Job done.Jon AlsburySenior Web Services DeveloperLondon Metropolitan University

  10. Marcus Whitworth says:

    Hi Jon,I had taken your advice, and had the trimflt servlet installed on our dev server for the past few months without a problem… until now.Nothing wrong with performance, moreso just a case of it begin too effective! I was entering double line breaks into a textarea field and saving it to the database. When I retrieved the record and displayed it back in a text field, all the double line breaks were converted to single ones.After about an hour of trying to find out what was causing it, I realised that in the resulting page html, all double line breaks had been converted to single – even those within the textarea tags.Does anyone have any suggestions for a workaround? I have removed the servlet for now, but would like to keep using it!thanks,Marcus.

  11. Scott Stroz says:

    If you are using multiple instances of CF, whre is the web.xml file located?

  12. Claude says:

    Marcus,take a look at how to exclude some files from being processed at:,Each instance has its own web.xml file under /servers//cfusion-war/WEB-INF/Claude.

  13. Byron says:

    I’ve found that trimflt was interfering with the functioning of CFADMIN.The ability to administer logs and DSN’s was affected somehow. Once removed the CFADMIN returned to normail functioning.It also affected the ability of homesite and dreamweaver to display the list of databases in the database tab/panels.While I really love the idea of controlling whitespace it seems that trimflt may not be ‘the’ answer, yet.B

  14. Allan says:

    Here’s an interesting scenario:I’ve been using the “Enable Whitespace Management” option in the admin app, and it’s done a decent job – I would rate it 95 out of 100.I came across a situation today where I had a page display queried data (eventual destination to be a text file, not the screen), with whitespace being used to align the data… I’ve run into a situation where I actually need the whitespace.Not sure I want to turn off the system-wide “Enable Whitespace Management” option, but then again, I absolutely need to output the proper number of “space” characters.Just thought I’d share this before I hope back on the Google train in search of a solution.[CFMX 6.1 on IIS]