Parsing XML As Query Object In Coldfusion

Get the WebProNews Newsletter:

[ Business]

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.

<?xml version="1.0"?>
<contact category="friend">
<contact category="friend">
<contact category="enemy">
<contact category="co-worker">
<contact category="friend">

Reading in the xml is short and sweet simply by using this little block of code.

method="GET" resolveurl="No" ></cfhttp>
<cfset mydoc = XmlParse(CFHTTP.FileContent)>

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.

<cfset pb = http://mydoc.phonebook.XmlChildren>
<cfset size = ArrayLen(pb)>

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.

<cfset myquery = QueryNew("cat, firstName, lastName, phone, email")>

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.

<cfset temp = QueryAddRow(myquery, #size#)>

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.

<cfloop index="i" from = "1" to = #size#>
<cfset temp = QuerySetCell(myquery, "cat",
#mydoc.phonebook.contact[i].XMLAttributes['category']#, #i#)>
<cfset temp = QuerySetCell(myquery, "firstName",
#mydoc.phonebook.contact[i].firstName.XmlText#, #i#)>
<cfset temp = QuerySetCell(myquery, "lastName",
#mydoc.phonebook.contact[i].lastName.XmlText#, #i#)>
<cfset temp = QuerySetCell(myquery, "phone",
#mydoc.phonebook.contact[i].phone.XmlText#, #i#)>
<cfset temp = QuerySetCell(myquery, "email",
#mydoc.phonebook.contact[i].email.XmlText#, #i#)>

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.

<cfinvoke component="pbook_meths" method="sortLName"
<cfinvokeargument name="q_obj" value="#myquery#">

The output of the result from the function would be no different than that of a normal SQL query.

<table border=1 width=500 align=center>
<th>category</th><th>fist name</th><th>last
<cfoutput query="Result">
<td>#lastName#</td> <td>#phone#</td>

The function being called would be stored in its own file as components are in ColdFusion and would look like this.

<cffunction name="sortLName" access="remote"

<cfargument name="q_obj" required="Yes" >


<cfquery name="pbTest" dbType="query">
FROM arguments.q_obj
order by lastName,cat

<cfcatch type="Any">




<cfreturn pbTest>


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.


Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Ben Cortese is a developer and business
analyst for the financial industry and develops affiliate
powered websites. http://www.inetsite.net Copyright 2007.

Parsing XML As Query Object In Coldfusion
Top Rated White Papers and Resources
  • Guest

    For those of you who, like me, have to parse 25K line xml files (not very large files in the real world), you may find that Coldfusion maxes out and returns an ‘out of memory’ error.

    I am running Coldfusion Developer Edition on Win XP Pro with a 3GHz dual core AMD Athlon 64 X2 processor and 3+ gigs of RAM.

    It appears that since Cold-Fusion needs to allocate enough memory to store the structure for 100% of the XML file at once, there will be resource management issues when attempting to read/parse contents of large xml files.

    If this happens to you, you may want to look into one of the industry-standard SAX parsers.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom