Click to search Andy Jarrett.co.uk RSS feed

Loading Twitter

CFXML and CF tags gotcha

As i'm preparing to type this post what I'm about to says is so obvious its annoying. When you are using the <cfxml> tag you cannot have xml tags that begin with "cf" as Coldfusion tries to parse them. For example:

view plain print about
1<cfxml variable="xml1">
2    <myXml>
3        <cftest>my data</cftest>
4    </myXml>
5</cfxml>

Would fail with something like "A tag starting with 'CF' has been detected. This tag is not supported by this version of ColdFusion. Please verify your typo and try again. Unknown tag: cftest. "

The workaround:

Modify the tag from <cftest> to <ctest>

view plain print about
1<cfxml variable="xml2">
2<myXml>
3    <ctest>my data</ctest>
4</myXml>
5</cfxml>

The afterwards get regex to make the changes.

view plain print about
1<cfset xml2 = reReplace(xml2, "<(.*?)c", "<\1cf" , "all") />
To explain the regex.
  • Attribute 1, is the string your are modifying.
  • Attribute 2, is the regex here. Here I am using creating a back reference (think of it as a variable) to catch the closing tag backslash.
  • Attribute 3, is the same as att. 2 but we remove the brackers and call the back reference via "\1".
  • Tells us to capturing all occurances.

Comments Comments (1) | Print Print | Send Send | 4841 Views

If you like what you see on the website and/or this post has helped you out in some way please consider donating to help keep me in beer vodka. The donations are made through Paypal, which accepts almost any credit card or eCheck.

(Comment Moderation is enabled. Your comment will not appear until approved.)
More readable and less processing overhead would be:
<cfset cf = "cf" />
<cfxml variable="myXml">
<#cf#myTag>...</#cf#myTag>
BlogCFC by Raymond Camden + Twitter @AndyJ + ColdFusion jobs + Contact Me + Snippets/Downloads + RSS .