Working with funky XML in ColdFusion

This post is more than 2 years old.

Yesterday I blogged about writing an interface to the kuler API. A user wrote me and mentioned he had tried to do the same, but ran into a problem with the XML. Kuler's feeds include XML that may look a bit odd. Consider this snippet:

    &lt;!-- theme title --&gt; 
    &lt;title&gt;Theme Title: my theme&lt;/title&gt; 
    &lt;!-- url link to theme within the kuler application --&gt; 
    &lt;enclosure xmlns=""&gt; 
    &lt;title&gt;Perfect Fit&lt;/title&gt; 
    &lt;link length="1" type="image/png"&gt;

&lt;!-- description content which includes theme png, artist, posted date, tags, and swatch hex colors --&gt; 

    &lt;!-- themeID --&gt; 
    &lt;!-- theme title --&gt; 
    &lt;kuler:themeTitle&gt;my theme&lt;/kuler:themeTitle&gt; 
    &lt;!-- url link to theme's png image --&gt; 


While it starts off rather simple, you may notice the tags towards the end have colons in them. The user who wrote me had trouble working with them because he was trying to use struct/array notation like so:

<cfset someval = somenode.item.kuler:themeitem.xmlText>

Obviously this threw an error since colons aren't legal in ColdFusion variables. What he forgot though is that when you treat XML like a struct, you can use any struct notation you want - even bracket notation. The line above could be rewritten like so:

<cfset someval = somenode.item["kuler:themeitem"].xmlText>

Notice how the 'funky' XML tag (kuler:themeitem) is used as a string inside the brackets.

This also applies to any structure of course.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA