ColdFusion XML Object Will Auto-Convert To String Often, But Not Always

Posted July 2, 2007 at 5:08 PM by Ben Nadel

Tags: ColdFusion

I was just playing around with some ColdFusion XML RPC functionality when I accidentally treated the XML document object as a string, not as a complex value. Of course, the reason I didn't notice it at first is because ColdFusion will automatically convert the XML Document Object to an XML string when it is required for string manipulation. So for instance, the following code:

  • <!--- Create an XML value. --->
  • <cfxml variable="xmlGirl">
  •  
  • <?xml version="1.0" encoding="UTF-8"?>
  • <name>
  • Julia Stiles
  • </name>
  •  
  • </cfxml>
  •  
  • <!--- Output XML as string. --->
  • #xmlGirl#

... simply outputs the XML string:

  • <?xml version="1.0" encoding="UTF-8"?>
  • <name>
  • Julia Stiles
  • </name>

Likewise, when I try to run:

  • <!--- Get trimmed XML string. --->
  • <cfset xmlData = Trim( xmlGirl ) />

ColdFusion does not error. It just returns the trimmed XML string representation of the XML document object. This is kind of cool, but I am not sure how I feel about it. I generally don't like implicit data conversion as it can lead to problems when you expect one thing and get another. For instance, if you pass the ColdFusion XML document object to the function, ToBase64():

  • <!--- Get the Base64 encoding of the XML. --->
  • #ToBase64( xmlGirl )#

... ColdFusion throws the following error:

Could not convert the value of type class coldfusion.xml.XmlNodeList to binary.

The problem here is that ToBase64() can take either a string OR a binary object. I guess it first tries to convert the XML document object to binary before it tries to convert it to a string. So, where as everything was copasetic as an argument to one function, it bombs out as an argument to another function.

So, while I didn't realize that ColdFusion automatically converts XML document objects to strings when necessary, I would still suggest actually calling the ToString() method (and passing in the XML object) when you need the string representation:

  • <!--- Get string value for XML document. --->
  • #ToString( xmlGirl )#

That way, there are no surprises.



Reader Comments

Jul 2, 2007 at 10:30 PM // reply »
1 Comments

That CF8 banner if f'ing hilarious!
Oh and good post on the xml...


Jul 3, 2007 at 7:21 AM // reply »
11,314 Comments

@Russ,

Thanks on both counts. I got bored of serving up Google AdSense ads all the time, so I started making a few banner ads just for fun :) Gives me something to do when I don't feel in the coding mood.


Jun 15, 2011 at 7:18 PM // reply »
6 Comments

Ok, so I have some XML output that I need to be a number but ColdFusion is seeing it as a string. Here's the error:
The value coldfusion.xml.XmlNodeList cannot be converted to a number.

How can I convert this variable to a number? It outputs such as 6.80, but I can't do math with it since it's not a number.

Help, please :-)


Aug 31, 2011 at 2:59 PM // reply »
11 Comments

Ben, have you ever seen or encountered a case where using ToString() and CFFILE to change XML from an XML object to a string (and then write to a file) mangles the results?

Specifically, I've got a case where I am doing a great deal of chopping up a parsed XML file into separate strings that get written back to individual XML files. In the source XML it doesn't "look" like there is any special characters (although its possible there are,) but when I write the file, pieces the text get mangled as if there were specials characters that caused a character encoding issue.

ToString does not appear to support the encoding attribute when using it with XML, and the only other thing I can think of is to set the encoding type on the cffile write.

Any thoughts?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 20, 2013 at 2:17 AM
ColdFusion NumberFormat() Exploration
Nice read thanks Ben, Is there a way to mask a negative number? Long story short in the finance sector when you go 'short' on a stock you want the price to fall this is a good thing because you are ... read »
Jun 20, 2013 at 1:09 AM
The Beauty Of The jQuery Each() Method
my html code : <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="nss.js"> ... read »
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools