Server Time:
Sunday May 11 2008 08:15 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Parsing XML With ColdFusion MX
by: Drew Tempelmeyer
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

A lot of people want to integrate XML into their ColdFusion applications but they don't know how to parse it. Parsing XML with ColdFusion MX is really simple. If you know me and have seen some of my applications, I use cfscript a lot. I've just recently completed cfblog which is a ColdFusion blog system; once it was completed, I needed something else to do. I know I don't submit a lot of tutorials here, but usually when I submit one, it'll be good (maybe).

What we'll be using:
cfhttp
xmlparse
QueryNew
QueryAddRow
QuerySetCell
cfquery
cfoutput
cfscript


To understand this tutorial, you will have to know the basic concpets of cfscript, and the Query functions.

I commented the code when I was writing it so in a way, it is its own tutorial :P

<cfhttp url="http://www.xp-resources.com/easycfm.xml" method="GET">

<cfscript>
    xmlfile = xmlparse(cfhttp.filecontent); //Parses the XML
    xmlsize = arraylen(xmlfile.Tutorials.xmlchildren); //Tutorials is the parent tree
    xmlqry = QueryNew("id, title, url, author, platform, descr"); //Sets a query for output
    QueryAddRow(xmlqry,xmlsize);

    for(a=1;a LTE xmlsize;a=a+1) {
        QuerySetCell(xmlqry,"title",xmlfile.Tutorials.TutorialID[a].Title.xmlText,a);
        //xmlfile.Tutorials.TutorialID[a].Title.xmlText gets the text of the title for the current tutorial (xmlText)
        QuerySetCell(xmlqry,"url",xmlfile.Tutorials.TutorialID[a].URL.xmlText,a);
        QuerySetCell(xmlqry,"author",xmlfile.Tutorials.TutorialID[a].Author.xmlText,a);
        QuerySetCell(xmlqry,"platform",xmlfile.Tutorials.TutorialID[a].Platform.xmlText,a);
        QuerySetCell(xmlqry,"descr",xmlfile.Tutorials.TutorialID[a].Description.xmlText,a);
        QuerySetCell(xmlqry,"id",xmlfile.Tutorials.TutorialID[a].xmlAttributes.id,a);
        //xmlfile.Tutorials.TutorialID[a].xmlAttributes.id gets the value of the attribute in the tag
    }
</cfscript>

<cfquery name="tutorials" dbtype="query">
    SELECT        *
    FROM           xmlqry
    ORDER BY     id DESC
</cfquery>

<cfoutput query="tutorials">
    <a href="#url#" target="_blank">#title#</a> - #descr#
    <br>
    --Author: #author#
    <br><br>
</cfoutput>


Questions? Comments? Need Help? Contact Drew Tempelmeyer.

Date added: Sun. November 9, 2003
Posted by: Drew Tempelmeyer | Views: 16465 | Tested Platforms: CFMX | Difficulty: Intermediate
Categories Listed: XML

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Creating Static HTML Pages from Dynamic Pages
Static HTML can help reduce the load on the server. This helps resources be used for other things that you may possibly need. Creating static HTML in ColdFusion is a simple task and can simply be done by following this tutorial. - Date added: Thu. March 31, 2005
9 Day Weather Forecast
This will retrieve the 9 day weather forecast for the specified ZIP code. - Date added: Fri. May 16, 2003
WHOIS Search
This will show you how to make a simple WHOIS search that can be easily added to any site. - Date added: Sun. April 27, 2003
Shout Box [LIVE]!
Ever want a place where people can chat or post their opinions? Ever heard of a shout box? This tutorial will give you an easy example of having a shout box on your site. - Date added: Sat. February 22, 2003
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
In what case would you use XML with ColdFusion
In what case would you use XML with ColdFusion? I am thinking the only situation you would use XML instead of a database is on CD. However, if it is on CD, you can't use ColdFusion. You need to have access to a coldfusion server to process coldfusion page. Can you or someone give me several situations where XML would be used instead of database if you have a database?

Johnny
Posted by: Johnny
Posted on: 04/08/2004 10:05 PM
When to use XML
XML should be used when you want to deliver information to third-party easily... Take into account the Tutorial Feeds on this site:

http://www.easycfm.com/syndication/EasyFeed.xml

They allow people who run ColdFusion related sites to display tutorials on their site easily...

You can also keep information in a temporary XML structure that will contain specific information about a logged in user...

Truth be told.. the possibilities are pretty much endless.

XML was not created to replace the database.. it was created to allow the exchange of information to be easier.. since all XML ends up being processed the same, etc.. so it's a standard that is uses to create and parse data between two systems!
Posted by: Pablo Varando
Posted on: 04/14/2004 10:33 AM
simplification
thanks for the example, I hadn't used xml in cf yet, and I had to retrieve 3rd party content as you explained in last post.
I have simplified your code a bit to avoid headaches :))
I put it here in case someone would reuse your example:

<cfhttp url="http://www.xp-resources.com/easycfm.xml" method="GET">
<!--- using this list instead of enumeration --->
<cfset fieldsList = title,url,author,platform,description,xmlAttributes">

<cfscript>
xmlfile = xmlparse(cfhttp.filecontent); //Parses the XML
xmlsize = arraylen(xmlfile.Tutorials.xmlchildren); //Tutorials is the parent tree
xmlqry = QueryNew(#fieldsList#); //Sets a query for output
QueryAddRow(xmlqry,xmlsize);

for(a=1;a LTE xmlsize;a=a+1) {
for(b=1;b LT listLen(fieldsList);b=b+1) {
QuerySetCell(xmlqry,listGetAt(fieldsList, b),xmlfile.Tutorials.TutorialID[a][listGetAt(fieldsList, b)].xmlText,a);
}

}
</cfscript>
<cfquery name="tutorials" dbtype="query">
SELECT *
FROM xmlqry
ORDER BY eventid asc
</cfquery>

<cfoutput query="tutorials">
<cfloop list="#fieldsList#" index="zzz">
#zzz# #evaluate(zzz)#<br>
</cfloop>
</cfoutput>
Posted by: pim
Posted on: 08/26/2004 06:52 AM
simplification(2)
little correction :

<cfquery name="tutorials" dbtype="query">
SELECT *
FROM xmlqry
ORDER BY eventid asc
</cfquery>

should be

<cfquery name="tutorials" dbtype="query">
SELECT *
FROM xmlqry
ORDER BY id desc
</cfquery>
Posted by: pim
Posted on: 08/26/2004 06:54 AM
Parse XML using ColdFusion 4.5
I tried using this tutorial with CF 4.5 and got a <cfquery> error becuase no datasource was declared. So I put it on our Web server, which is running 5.0. Can XML be parsed using this version?

Pat
Posted by: Pat
Posted on: 09/21/2004 10:03 AM
Query of Queries
4.5 doesn't support query of queries so the dbtype of query is what's breaking it for you...
Posted by: Jamie Martin
Posted on: 10/11/2004 02:04 PM
XML to SQL
I am trying to take an XML file into SQL so that I can run queries on it. We collect stats from one company, put the data into our database and run reports off it. The reports have several calculations so I don't feel that running xml queries is the best method.

The data file is pure XML. I am not sure how to get into a format that can be insert into a table?

can anyone help?

Posted by: David Humphrey
Posted on: 11/17/2004 06:43 PM
Where's the XML file?
Can you send me the XML file. The URL seems to be out dated
Posted by: JacktheRipper
Posted on: 11/18/2004 04:48 PM
Post the answer please
I'd like to see that solution. I have an XML from ebay that i want to dump into a mySQL database. I don't know anything about XML. help.
Posted by: Rob
Posted on: 11/20/2004 02:34 PM
XML -> SQL how'd ya do it?
What was the solution to the XML-SQL Problem? is there an easy way to do it? Has anyone here used SQLXML? is there a clean way to do it in CF?
Posted by: Jamie Martin
Posted on: 12/13/2004 01:36 PM
XML->SQL Use example, replace setcell
replace setcell with insert query, pretty much (first example is pretty easy to read)
Posted by: d
Posted on: 03/15/2005 01:55 AM
Feedburner
I posted this elsewhere, but maybe it can get answer here too.

These parsing things break on Feedburner feeds because instead of
<link>URL</link>
in the XML, it has
<link href="LINK" />
so you get " " instead of "http://yourlink.com" for the output. (Link is an empty tag in their feed.)

Ideas?
Posted by: nikole
Posted on: 03/18/2007 02:52 AM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
Daily Razor - ColdFusion Hosting

You are 1 of 713 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb