Generating XML from a CFC? Watch out for whitespace

A friend sent in a question this weekend that confounded him to no end. He was generating some XML but kept getting an error in the browser because of extra white space. He had used cfsetting enablecfoutputonly=true. He had used output=false in the method. But he still had a white space issue. And let's face it - ColdFusion likes whitespace more than Paris Hilton likes publicity.

Turns out he had simply forgotten one last hole to plug - the cfcomponent tag. It takes an output=true/false attribute to specify if the initialization area should generate output. If that sounded Greek to you - think of it like this. Any line of code (or white space) outside of cffunction tags is run when the CFC is created. So imagine this CFC:

<cfcomponent displayName="Paris Hilton">

<cffunction name="getTrashier" returnType="void"> <!--- code here ---> </cffunction>


The two blank lines outside of the cffunction tag pair will be part of the output when the CFC is created. To remove it - just add the output tag I mentioned above:

<cfcomponent displayName="Paris Hilton" output="false">

<cffunction name="getTrashier" returnType="void"> <!--- code here ---> </cffunction>


Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA