Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at RIA Unleashed (Nov. 2010) with:

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

By Ben Nadel on
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

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

Reply to this Comment

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 :-)

Reply to this Comment

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?

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.