Reminder on structs and bracket notation

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: <cfset CheckOut = xmlParse(GoogleCheckOutFile)> #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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. 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 https://www.raymondcamden.com

Comments