Unqualified XML's and cfheader

I've been playing around with creating an XML file this morning from a plain .cfm page and came across an annoyance. Now I've done this loads but this morning I was creating my own Ajax functions which meant the cfm page had to return a fully qualfied as XML which was passed into Javascripts XML parser, but Js was passing an error. The test code was simple

[xml.cfm]

view plain print about
1<cfheader name="Content-Type" value="text/xml">
2<?xml version="1.0" encoding="UTF-8"?>
3<root>
4 <node>
5 <child>1</child>
6 </node>
7<root>

The problem was CFHEADER adds a couple of blank lines to the top of the page meaning that the page did not pass as XML. Well thanks to Rob Wilkerson on CF-Talk the anser was obvious simple - use CFCONTENT

view plain print about
1<cfcontent type="text/xml" reset="Yes" />
2<?xml version="1.0" encoding="UTF-8"?>
3<root>
4 <node>
5 <child>1</child>
6 </node>
7<root>

I did try a few other ways of surpressing the white space usin a variety of methods but nothing came close.

Posted: 30-Mar-2006

View: 4429

Permalink: here

Comments

I am getting on the AJAX bandwagon late but I guess I better keep this tip in mind.

#1 Adedeji Olowe
30/Mar/06 4:36 PM

I came across this cause I was playing around with my own implementations. I havent played around with CFAJax etc for a while but from what I remember this isnt an issue with them (I might be wrong) so don't forget to check em out.

#2 Andy J
30/Mar/06 4:55 PM

I've been doing a lot of XML/XSLT work lately and have found a couple of other mind-bending tweaks that I've had to track down and find:

1. If your ajax response bombs, alert the responseText. I found the responseXML returning null because the XML returned with whitespace in front of the xml declaration. Wrapping the code in <cfprocessingdirective suppresswhitespace="true">..</cfprocessingdirective> fixed that. was a beast to track down, though.

2. When creating XML documents using <cfxml> all whitespace is preserved when the toString() method is called on it. This left little gaps between values and their tags because I wanted to format the xml nicely for readability. Then those gaps caused issues in my XSL because my "value" did not equal 'YES', but equalled 'YES ' instead. Ugh. Using normalize-space() in my XSLT took care of that.

No doubt there are a thousand other little things I haven't run into yet, but those are a couple that took me a while to find so maybe my pain can help someone else.

#3 Rob Wilkerson
30/Mar/06 6:26 PM

this post helped me get out of a jam when transposing ajax examples from PHP to ColdFusion. thanks!

#4 Chris Vigliotti (hibiscusroto)
21/Aug/07 11:26 AM