I'm an absolute newbie (1 day so far) to Coldfusion, and I have to learn it for work. I come from a .NET background, so I'm trying to make sense of all this.
I essentially just want to read an XML file, and return it as JSON. Simple.
XML file structure:
<countries>
<country code="AU" name="Australia" />
<country code="NZ" name="New Zealand" />
<country code="US" name="United States" />
<country code="UK" name="United Kingdom" />
</countries>
I've created a REST service for which I'm using a CFC using cfscript. Code:
component restpath="locations" rest="true"
{
remote Array function getCountries() httpmethod="GET"
{
response = [];
xCountries = xmlParse(expandPath("/data/countries.xml"));
numItems = ArrayLen(xCountries.countries.XmlChildren);
for (i = 1; i LTE numItems; i++)
{
sCountries = StructNew();
sCountries.code = xCountries.countries.country[i].XmlAttributes.code;
sCountries.name = xCountries.countries.country[i].XmlAttributes.name;
arrayAppend(response, sCountries);
}
return response;
}
}
Using this technique, I achieve the correct response in my test, but I have this weird feeling it might be inefficient. I was just thinking that creating a new struct and adding it to an array in a loop (the full xml file is around about 100 items) could be clunky.
In fact, I'm not sure if using a struct is the best way to do this. In .NET, I'd have made a class, then added stuff to it's properties. I went in search of something similar to a class, and i came across something, and I created a Countries.cfc file:
component accessors="true" output="false"
{
property name="code" type="string";
property name="name" type="string";
}
The only problem I'm having with this, is that I have no idea how to use this. Do you use components like this for OOP? Or is the above 'struct' method better?
TL;DR; Is the above the best way to achieve this? Is there a best practice for this stuff in ColdFusion?
Any other pointers or advice is welcome. Thanks
IMO there's nothing wrong with the approach you've taken. It'd be familiar to any CF developer. There's no native XML -> Object mapping technology in CF (like JAXB in Java or XML Serialization or Data Binding in .NET) so you're going to end up with the read loop somewhere in your code no matter what you do. depending on how often you expect the country code list to change, you could cache the structure you want to return in the application scope, meaning you'll only have to parse the XML once.
In answer to the struct/object question, either work fine. Historically, object instantiation was expensive in CF so the struct approach was often favoured. The speed difference is now less pronounced, so either are valid.
My personal criteria for choosing object/struct is whether I need to use the data in multiple places. If it's something I'm going to be using throughout my app and possibly updating, then defining an object is worth it. If I'm just serving read-only data which is unique to a particular function, then defining objects feels like overhead.