| Home | Articles | Archive | Links |


- Misc
Parsing XML As Query Object...
Article Marketing Online
Find Address From Phone Number...
Find People On The Internet Getting...
Writing Up Sizzling Sales Copy That...
Keeping Your Children Safe On...
How To Properly Use A Uk Broadband...
List Building Don t Dupe
How To Promote Affiliate Products...
The Right Cosmetics Bag
The Right Musical Instrument Dealer
The Right Musical Instrument
Building Websites The Easy Way
Are Online Paid Surveys A Scam
Want Music Just Download It
Common Issues With Uk Broadband...
Build Ecommerce Web Site
Build Your Own Web Page For Free
Keyword Search Why Keyword Tools Are...
Disruptive Web Emergence...
Promoting Your Website
The Effectiveness Of Anchor Text...
Ezine Marketing Tips
- Design
Seo Three Design Elements That...
- SEO
Seo Application Tips For Yahoo
Seo Why Submitting To The Big...
How To Use Link Building For Great...
Seo Blocking Search Engines From...
Seo The Difference Between Black Hat...
Why Search Engine Optimization Is...
Seo What Is Spamdexing
Search Engine Optimization Why Is...
Seo How Trustrank Is Catching Seo...
Keep Visitors Returning To Your Web...
SEO Five SEO Do s
Seo Five Big Seo Don ts
Seo Copywriting Finding A Happy...
- Hosting
Is It Worth Getting An Affordable...
Web Hosting Forums The Best...
- Linking
Web Directories Used To Get One Way...
Post Your Links Everywhere To Build...

Parsing XML As Query Object In Coldfusion



I


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


...Archive >>

Submit Your Site
Recent Articles
  • Web Design Course – You Don’t Need to Go Back to College!

    A quality Web design course can arm you with the skills and knowledge for making big money as a Web designer, or just give you the confidence to build your own fabulous site with ease But you don’t have to spend big bucks and years of your time on a college degree to become a first rate Webmaster In fact there are multiple online Web design courses that will give you a basic, intermediate, and even expert level of skill in the Web development arena...

  • How to Recover Deleted Emails and Restore Them Completely

    It can happen to anyone We accidentally delete an email that we really need And it just happened to be important work file, attachment or picture...

  • Advantages of Wireless Internet

    Wireless internet or Wi-Fi is one of the latest developments in providing people with Internet access whenever they need it - even while traveling This is a convenient way to stay connected with family members or business clients anytime of the day With the fast evolution and advancement of the Internet, millions of people all over the world make use of the Internet in their daily life...

  • Online Gift Shopping

    If you are looking for a birthday gift, wedding gift or Christmas gift this year try shopping online and you will be pleasantly surprised by the extensive range of gift ideas available to you If you are stuck for something to get or struggling to find exactly what you want on the high street, online shopping is the perfect solution for you The world is your oyster when you shop online for those all important special birthday gifts, wedding gifts, personalised gifts or whatever the case may be...

    Copyright (c) 2008 Isnare.com. All rights reserved.

  • Google
    Parsing XML As Query Object In Coldfusion