|
|
|
Parsing XML As Query Object In Coldfusion
work in the financial course documented heavily. And industry as a developer and frankly it's hard to find the not that the financial extra time with a heavy work load industry has any greater and deadlines to meet to go off implementation of XML than any experimenting But I managed to other, but web applications that put aside some time to look at utilize back office processing parsing XML into an object that for loan applications, or new developers are all familiar with, account opening processes, or the Query object. As a developer, retail internet banking writing SQL is common place, so I applications all to some extent figured maybe looking at a hybrid make calls to a host. Many times approach to XML and SQL in it's a DB2 database, and many ColdFusion, might prove to offer times some other process that some benefit. returns account information or validation information in xml So I'll show an example of format. reading in a Phonebook xml document. It will look like this: One of the fun things that I You can copy this and save it off found working in ColdFusion is as phonebook.xml if you like. the many ways you can manipulate xml data. I'm certain this type <?xml version="1.0"?> of process isn't exclusive to <phonebook> ColdFusion, but I wanted to put <contact category="friend"> it out there anyway. Many <firstName>John</firstName> developers have a little <lastName>Smith</lastName> apprehension about working with <phone>412-555-1212</phone> new technologies at first, or <email>johnsmith@email.com< trying something that hasn't been /email> already tried and true and of </contact>
<contact category="friend"> </phonebook> <firstName>Jane</firstName> <lastName>Smith</lastName> Reading in the xml is short and <phone>412-555-1212</phone> sweet simply by using this little <email>janesmith@email.com< block of code. /email> </contact> <cfhttp <contact category="enemy"> url="http://www.mywebsite.com/myx <firstName>Bob</firstName> mldocs/phonebook.xml" <lastName>Jones</lastName> method="GET" resolveurl="No" <phone>412-555-1213</phone> ></cfhttp> <email>bob-jones@mailserver.co <cfset mydoc = m</email> XmlParse(CFHTTP.FileContent)> </contact> <contact category="co-worker"> Now you've got the xml object in <firstName>Bill</firstName> a defined variable name called <lastName>Johnson</lastName "mydoc". > <phone>412-555-1214</phone> Next you set a variable to <email>bill.johnson@someserver contain the child element nodes .com</email> of the xml document and do the </contact> same to define the size of the <contact category="friend"> document, which you will see in a <firstName>Jack</firstName> bit as to what it's used for. <lastName>Robinson</lastNam e> <cfset pb = <phone>412-555-1215</phone> http://mydoc.phonebook.XmlChildre <email>www.jackrobinson.com< n> ;/email> <cfset size = ArrayLen(pb)> </contact>
Now create a query object with object like an array. the phonebook data. As you can see we already know what the node <cfloop index="i" from = "1" properties and names are to to = #size#> create the myquery object <cfset temp = containing the xml "column QuerySetCell(myquery, "cat", names". I say column names to #mydoc.phonebook.contact[i].XMLAt relate the data to a structure tributes['category']#, #i#)> like a database. <cfset temp = QuerySetCell(myquery, <cfset myquery = "firstName", QueryNew("cat, firstName, #mydoc.phonebook.contact[i].first lastName, phone, email")> Name.XmlText#, #i#)> <cfset temp = Now that we have the "Columns" of QuerySetCell(myquery, "lastName", the query object, we use the Size #mydoc.phonebook.contact[i].lastN object to give us the parameters ame.XmlText#, #i#)> to define the Rows like this. <cfset temp = QuerySetCell(myquery, "phone", <cfset temp = #mydoc.phonebook.contact[i].phone QueryAddRow(myquery, #size#)> .XmlText#, #i#)> <cfset temp = Now you've established a Query QuerySetCell(myquery, "email", Object 'temp'. #mydoc.phonebook.contact[i].email .XmlText#, #i#)> Now comes the part where you fill </cfloop> the Query Object with the data from the XML document. Loop Essentially what you've just done through every "row" in the object is take the xml and turn it into adding its value from the xml the same type of data structure
you would have when you query a name</th><th>phone</th>& database. lt;th>email</th> <cfoutput query="Result"> The rest should look familiar to <tr> many ColdFusion developers who <td>#cat#</td><td>#first use ColdFusion Components which Name#</td> is a great practice. <td>#lastName#</td> <td>#phone#</td> Call the query function using the <td>#email#</td> CFINVOKE. This particular </tr> function is if you were to sort </cfoutput> the phonebook individuals by last </table> name. The function being called would <cfinvoke be stored in its own file as component="pbook_meths" components are in ColdFusion and method="sortLName" would look like this. returnVariable="Result"> <cfinvokeargument name="q_obj" <cffunction name="sortLName" value="#myquery#"> access="remote" </cfinvoke> returnType="query"> The output of the result from the <cfargument name="q_obj" function would be no different required="Yes" > than that of a normal SQL query. <cftry> <table border=1 width=500 align=center> <cfquery name="pbTest" <th>category</th><th>fis dbType="query"> t name</th><th>last SELECT *
FROM arguments.q_obj order by lastName,cat Your component can contain </cfquery> numerous functions manipulating the xml query object just the <cfcatch type="Any"> same as you would a normal query from a database. You don't have <P><cfoutput>#cfcatch.messa the power that a relational ge#</cfoutput></P> database offers of course, but if you're comfortable writing SQL, </cfcatch> you may find yourself hitting the ground running with working with </cftry> xml in ColdFusion. <cfreturn pbTest> Thanks and Happy Coding. </cffunction>
About the Author:
Ben Cortese is a developer and business analyst for the financial industry and develops affiliate powered websites. Learn more at InetSite.net Copyright 2007. Article can be reprinted only if unchanged.
Read more articles by: Benjamin Cortese
Article Source: www.iSnare.com |
|