Parsing XML With ColdFusion MX
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.


All ColdFusion Tutorials By Author: Drew Tempelmeyer
  • 9 Day Weather Forecast
    This will retrieve the 9 day weather forecast for the specified ZIP code.
    Author: Drew Tempelmeyer
    Views: 16,098
    Posted Date: Friday, May 16, 2003
  • 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.
    Author: Drew Tempelmeyer
    Views: 9,873
    Posted Date: Thursday, March 31, 2005
  • Parsing XML With ColdFusion MX
    Want to know how to parse XML with ColdFusion with ease and very basic concepts? Then this is the tutorial for you.
    Author: Drew Tempelmeyer
    Views: 16,554
    Posted Date: Sunday, November 9, 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.
    Author: Drew Tempelmeyer
    Views: 19,947
    Posted Date: Saturday, February 22, 2003
  • WHOIS Search
    This will show you how to make a simple WHOIS search that can be easily added to any site.
    Author: Drew Tempelmeyer
    Views: 9,165
    Posted Date: Sunday, April 27, 2003