Parsing XML As Query Object In Coldfusion
Parsing XML As Query Object In Coldfusion
I work in the financial industry as a developer and not that the financial industry has any greater implementation of XML than any other, but web applications that utilize back office processing for loan applications, or new account opening processes, or retail internet banking applications all to some extent make calls to a host. Many times it’s a DB2 database, and many times some other process that returns account information or validation information in xml format.
One of the fun things that I found working in ColdFusion is the many ways you can manipulate xml data. I’m certain this type of process isn’t exclusive to ColdFusion, but I wanted to put it out there anyway. Many developers have a little apprehension about working with new technologies at first, or trying something that hasn’t been already tried and true and of course documented heavily. And frankly it’s hard to find the extra time with a heavy work load and deadlines to meet to go off experimenting But I managed to put aside some time to look at parsing XML into an object that developers are all familiar with, the Query object. As a developer, writing SQL is common place, so I figured maybe looking at a hybrid approach to XML and SQL in ColdFusion, might prove to offer some benefit.
So I’ll show an example of reading in a Phonebook xml document. It will look like this: You can copy this and save it off as phonebook.xml if you like.
John
Smith
412-555-1212
johnsmith@email.com
Jane
Smith
412-555-1212
janesmith@email.com
Bob
Jones
412-555-1213
bob-jones@mailserver.com
Bill
Johnson
412-555-1214
bill.johnson@someserver.com
Jack
Robinson
412-555-1215
www.jackrobinson.com
Reading in the xml is short and sweet simply by using this little block of code.
Now you’ve got the xml object in a defined variable name called “mydoc”.
Next you set a variable to contain the child element nodes of the xml document and do the same to define the size of the document, which you will see in a bit as to what it’s used for.
Now create a query object with the phonebook data. As you can see we already know what the node properties and names are to create the myquery object containing the xml “column names”. I say column names to relate the data to a structure like a database.
Now that we have the “Columns” of the query object, we use the Size object to give us the parameters to define the Rows like this.
Now you’ve established a Query Object ‘temp’.
Now comes the part where you fill the Query Object with the data from the XML document. Loop through every “row” in the object adding its value from the xml object like an array.
Essentially what you’ve just done is take the xml and turn it into the same type of data structure you would have when you query a database.
The rest should look familiar to many ColdFusion developers who use ColdFusion Components which is a great practice.
Call the query function using the CFINVOKE. This particular function is if you were to sort the phonebook individuals by last name.
The output of the result from the function would be no different than that of a normal SQL query.
category | fist name | last name | phone | |
---|---|---|---|---|
#cat# | #firstName# | #lastName# | #phone# | #email# |
The function being called would be stored in its own file as components are in ColdFusion and would look like this.
SELECT *
FROM arguments.q_obj
order by lastName,cat
#cfcatch.message#
Your component can contain numerous functions manipulating the xml query object just the same as you would a normal query from a database. You don’t have the power that a relational database offers of course, but if you’re comfortable writing SQL, you may find yourself hitting the ground running with working with xml in ColdFusion.
Thanks and Happy Coding.