A friend sent in an interesting problem yesterday that had a quick solution:
Need some help, I'm stuck on parsing this xml string. Hoping that you might kick me back in right direction. It seems to be getting stuck on new-order-notification with the following error:Diagnostics: Element NEW is undefined in CHECKOUT.
From playing around, I can do a rereplace on the - and get to work, but that is not really a good solution. So the question is, how can I use XML like the one below.
Code:
#checkOut.new-order-notification.google-order-number.XMLtext#
One of the nice things about ColdFusion's handling of XML data is that it lets you treat the document as a structure. This makes using XML very easy. However, you have to follow the same rules you would for a "normal" structure, and one of them is that you cannot use dot notation for keys that aren't valid ColdFusion variables.
Note the variable: #checkOut.new-order-notification.google-order-number.XMLtext#
The second and third keys are not valid variables, and therefore ColdFusion throws an error. Luckily this is trivial to get around:
#checkOut["new-order-notification"]["google-order-number"].XMLtext#
By switching to bracket notation, ColdFusion will no longer throw an error.
Archived Comments
I think that when using bracket notation, you'll also have to watch out for what case you're using (upper/lower). I ran into an instance a while back where I was using bracket notation like ["firstname"] but the XML doc actually had "FIRSTNAME" as the node name.
Also not that even with square brackes that is a but with Nodes that start with an underscore. You get a java.lang.string error when trying to reference it. I have example code on my blog - http://www.danield.us/blog/ Along with a workaround.
I posted this on Daniel's blog, but I thought I'd share here too. I've not been able to reproduce the code he had that threw an error. (This is on CF7.0.2.)
Ray, I just upgraded my dev enviroment to 7.0.2 and still seeing same error mentioned above. Also tried a clean install of JRun and CF7.02 same thing. What JVM are you running and what OS. This is on Windows with Java Version 1.4.2_09