XML parsing in Coldfusion has improved a lot in the last few versions but converting a complex XML object to a Coldfusion structure is still a struggle I beleive. There are a few custom tags out there for doing this but the ones that I have used or know are all only for simple structures, not nested or subnested structures. So I use this little function I and one of my other friends worked on and it works like a charm.
Update (27/12/2008) : The new code has been upload on riaforge and the usage is now very simple . It now takes the XML file and converts to Struct without doing all the Xpath’s before the function call. Here is a sample usage.
<cffile action="read" file="#ExpandPath('books.xml')#" variable="myBooks">
<cfdump var = "#ConvertXmlToStruct(ToString(myBooks), structnew())#">
The new code is here as well – Xml2Struct CFC
Anything below is now outdated :-
Update : Project can now be downloaded from here
A sample usage of the fucntion is below :-
Read the XML file into a Coldfusion variable, this can be a from a file locally or via a CFHTTP call or a webservice call.
<cffile action="read" file="#ExpandPath('books.xml')#" variable="myBooks">
<cfset myXml = XmlParse(myBooks) />
You need to know the root element of the XML document and pass that to the function.
<cfset xmlBody= xmlSearch(myXml,"//catalog") />
Then call the function to convert it to a Structure.
<cfdump var = "#ConvertXmlToStruct(responseBody[1], structnew())#">
The function itself can be found here.
The only problem that I notice about this is if the root element has any default namespaces (“xmlns=’blabla’) , the XmlSearch doesnt recognise it. Apart from that, it works like a charm.
The XML file used in this sample can be found here books sample xml file .
#1 by Brad Severance on April 21, 2009 - 6:59 pm
Quote
Hello Anuj,
Thanks for the awesome .cfc!
I have a quick question. Suppose you have an array inside a struct which only has one element. By default it appears that your function will turn that array into a struct within a struct. Is it possible to force it to make an array within a struct, even though that array has only one element?
Thanks!
#2 by Anuj Gakhar on April 22, 2009 - 5:03 pm
Quote
@Brad, I will try and re-visit the CFC today/tomorrow. I think there are a few things I can change around and optimize. I will let you know.
#3 by Brad Severance on April 22, 2009 - 6:48 pm
Quote
Thanks Anuj! I’m not smart enough to figure it out, but I was thinking that somehow how you could program it so that if you use a preset node name, that it would automatically make it an array. For example,
this will be an array
So, if someone uses “arelement” (or something similar), then the function would process that as an array.
This is just a thought.
Thanks again!!
brad
#4 by Brad Severance on April 22, 2009 - 6:51 pm
Quote
oops, looked liked it deleted the code; bout
-books-
-arelement-This is an array-/arelement-
-/books
(replace – with )
#5 by James Basco on July 10, 2009 - 4:46 pm
Quote
Thanks for the great code!
Works perfect for a quick way to consume an XML webservice then turn around and return a JSON object.
#6 by Dave on September 8, 2009 - 6:04 pm
Quote
Hi Anuj,
Brad posted this awhile back:
“Hello Anuj,
Thanks for the awesome .cfc!
I have a quick question. Suppose you have an array inside a struct which only has one element. By default it appears that your function will turn that array into a struct within a struct. Is it possible to force it to make an array within a struct, even though that array has only one element?”
I have run into the same need. The inconsistency caused by it creating an array within a stuct followed by a struct within a struct makes it hard to use the output. Any chance you can modify this to always create an array within a struct even though the stuct may only have 1 element?
#7 by Anuj Gakhar on September 8, 2009 - 7:21 pm
Quote
Hi Dave, That should be easy to change. I will have a look and get back. Give me a couple of days though.
#8 by Dave on September 9, 2009 - 3:09 pm
Quote
Anuj,
Thanks, I appreciate it. Your code will solve a problem on a project I’m working on. Really appreciate your sharing your work.
#9 by Dave on September 10, 2009 - 10:03 pm
Quote
Hi Anuj,
I hate to ask because I know that you’re doing me a favor, but is there any chance you might be able to make the modification to the code today? If it doesn’t look like you’ll be able to get to it, I’ll have to figure something else out. My project needs to be done by the morning. Just thought I would ask to see if you might have time.
Thanks!
Dave
#10 by Anuj Gakhar on September 11, 2009 - 8:37 am
Quote
Hi Dave, Sorry I have just got back online. I am afraid I wont have any time before the weekend. But it should be a pretty simple change to do and you can give it a shot and try doing it yourself meanwhile. Otherwise I will have a look during the weekend sometime.
#11 by Dave on September 11, 2009 - 9:06 pm
Quote
Hi Anuj,
I spent the entire day trying to make the change. I put displays in all over the place and still can’t quite follow the logic.
If you’re able to make the change this weekend, I’d greatly appreciate your help.
#12 by Dave on September 13, 2009 - 10:04 pm
Quote
Hi Anuj,
Hope your weekend is going well. Still look like you might have time to work on this today?