Converting a query into an XML feed using the CFFEED tag in ColdFusion

Readability

Converting a query into an XML feed using the CFFEED tag in ColdFusion

The fol­low­ing exam­ple shows how you can con­vert a data­base query into an XML feed by using the <CFFEED> and set­ting the action attribute to “create”.


    SELECT a.ARTID, a.ARTNAME, a.DESCRIPTION, a.ISSOLD, m.MEDIATYPE
    FROM ART a, MEDIA m
    WHERE a.MEDIAID = m.MEDIAID
    ORDER BY a.ISSOLD ASC, a.PRICE DESC



    feedMeta = structNew();
    feedMeta.description = "ColdFusion Art Gallery XML Feed";
    feedMeta.link = "http://coldfusionexamples.com/";
    feedMeta.title = "Art";
    feedMeta.version = "rss_2.0";
    
    colMap = structNew();
    colMap.content = "DESCRIPTION";
    colMap.rsslink = "ARTID";
    colMap.title = "ARTNAME";





The pre­ced­ing code pro­duces the fol­low­ing output:



    
        Art
        http://coldfusionexamples.com/
        ColdFusion Art Gallery XML Feed
        
            Enchanted Tree
            29
            Pastels
        
        
            Paradise
            37
            Pastels
        
        
            Mystery
            36
            Pastels
        
    

The following example shows how you can convert a database query into an XML feed by using the <CFFEED> and setting the action attribute to “create”.

<cfquery name="getArt" datasource="cfartgallery" maxrows="3">
    SELECT a.ARTID, a.ARTNAME, a.DESCRIPTION, a.ISSOLD, m.MEDIATYPE
    FROM ART a, MEDIA m
    WHERE a.MEDIAID = m.MEDIAID
    ORDER BY a.ISSOLD ASC, a.PRICE DESC
</cfquery>
 
<cfscript>
    feedMeta = structNew();
    feedMeta.description = "ColdFusion Art Gallery XML Feed";
    feedMeta.link = "http://coldfusionexamples.com/";
    feedMeta.title = "Art";
    feedMeta.version = "rss_2.0";
 
    colMap = structNew();
    colMap.content = "DESCRIPTION";
    colMap.rsslink = "ARTID";
    colMap.title = "ARTNAME";
</cfscript>
 
<cffeed action="create"
        query="#getArt#"
        columnMap="#colMap#"
        properties="#feedMeta#"
        xmlVar="feedXML" />
 
<cfdump var="#feedXML#" />

The preceding code produces the following output:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"
        version="2.0">
    <channel>
        <title>Art</title>
        <link>http://coldfusionexamples.com/</link>
        <description>ColdFusion Art Gallery XML Feed</description>
        <item>
            <title>Enchanted Tree</title>
            <link>29</link>
            <description>Pastels</description>
        </item>
        <item>
            <title>Paradise</title>
            <link>37</link>
            <description>Pastels</description>
        </item>
        <item>
            <title>Mystery</title>
            <link>36</link>
            <description>Pastels</description>
        </item>
    </channel>
</rss>

Leave a Reply

Your email address will not be published.