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:
Launch code in new window » Download code as text file »
... simply outputs the XML string:
Launch code in new window » Download code as text file »
Likewise, when I try to run:
Launch code in new window » Download code as text file »
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():
Launch code in new window » Download code as text file »
... 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:
Launch code in new window » Download code as text file »
That way, there are no surprises.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Lenny And Bo, ColdFusion Programmers (Vol. 23)
jQuery v1.1.3 Now 800% Faster (At The Same Size File)!
That CF8 banner if f'ing hilarious!
Oh and good post on the xml...
Posted by russ on Jul 2, 2007 at 10:30 PM
@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.
Posted by Ben Nadel on Jul 3, 2007 at 7:21 AM