XML to Javascript Object
I've been using ColdFusion since umm... 1996?, a long time anyway, so you'd think I'd know everything that there is to know about CF, its tags and functions. ;)
I jest, of course. There is always something new to learn that's exactly what I found out the other day.
I've just written a simple AJAX widget to go on a client's site to enable them to have a simple question/action work flow on their site. You know, the "Have you done this? Y/N" "If Yes do this, If No ask next question" kind of thing. It was pretty small workflow and I was contemplating simply hardcoding the whole thing. After a chat with a colleague to try and straighten some ideas out in my mind I decided to have an XML config file that contained the steps in the workflow.
Because the workflow is pretty simple I didn't really want to be requesting back to the server on each step, so I wanted to get my XML into javascript. Historically I've done this using CFWDDX, but my colleague pointed me to ToScript! I love all the CF tags, but functions are so much cleaner, especially when you're somewhere in the middle of a component and ToScript is no exception.
The only flaw in the plan was that ToScript takes a structure and doesn't see an XML object as a structure. Fortunately, without too much hassle I found Anuj Gakhar's XMLToStruct function and in 4 lines of neat code I go from XML file to javascript object. probably nothing new to most of you, but its always fun to use functions you haven't used before or forgot existed.

There are no comments for this entry.
[Add Comment] [Subscribe to Comments]