Determining the number of items in an XML feed in ColdFusion

Readability

Determining the number of items in an XML feed in ColdFusion

In a pre­vi­ous exam­ple, “Pars­ing a remote XML feed using the CFFEED tag in Cold­Fu­sion”, we saw how you could parse a remote XML feed into an XML object using the <CFFEED> tag in Cold­Fu­sion by set­ting the action attribute to “read” and set­ting the source, and xmlVar attributes.

The fol­low­ing exam­ple shows how you can deter­mine the num­ber of items in an XML feed by using the <CFFEED> tag and arrayLen() method.




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

10

This can be use­ful if you want to parse a feed and then loop over the returned result, as seen in the fol­low­ing example:





    [#idx#] #rssFeed.item[idx].title#

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

[1] Pars­ing a remote XML feed using the CFFEED tag in Cold­Fu­sion
[2] Con­vert­ing a query into an XML feed using the CFFEED tag in Cold­Fu­sion
[3] Dis­play­ing a grouped query using nested CFOUT­PUT tags in Cold­Fu­sion
[4] Dis­play­ing a query in an HTML table using the CFTABLE tag in Cold­Fu­sion
[5] Get­ting a list of columns returned by a CFQUERY tag in Cold­Fu­sion
[6] Query­ing a query using the CFQUERY tag in Cold­Fu­sion
[7] Deter­min­ing if a vari­able is a query object in Cold­Fu­sion
[8] Round­ing num­bers in Cold­Fu­sion
[9] Spec­i­fy­ing the num­ber of records shown in a query using the CFDUMP tag in Cold­Fu­sion
[10] Spec­i­fy­ing a max­i­mum num­ber of results from a query using the CFQUERY tag in ColdFusion

In a previous example, “Parsing a remote XML feed using the CFFEED tag in ColdFusion”, we saw how you could parse a remote XML feed into an XML object using the <CFFEED> tag in ColdFusion by setting the action attribute to “read” and setting the source, and xmlVar attributes.

The following example shows how you can determine the number of items in an XML feed by using the <CFFEED> tag and arrayLen() method.

<cffeed action="read"
        source="http://coldfusionexamples.com/feed/"
        name="rssFeed" />
 
<cfdump var="#arrayLen(rssFeed.item)#" />

The preceding code produces the following output:

10

This can be useful if you want to parse a feed and then loop over the returned result, as seen in the following example:

<cffeed action="read"
        source="http://coldfusionexamples.com/feed/"
        name="rssFeed" />
 
<cfoutput>
<cfloop from="1" to="#arrayLen(rssFeed.item)#" index="idx">
    [#idx#] #rssFeed.item[idx].title#<br />
</cfloop>
</cfoutput>

The preceding code produces the following output:

[1] Parsing a remote XML feed using the CFFEED tag in ColdFusion
[2] Converting a query into an XML feed using the CFFEED tag in ColdFusion
[3] Displaying a grouped query using nested CFOUTPUT tags in ColdFusion
[4] Displaying a query in an HTML table using the CFTABLE tag in ColdFusion
[5] Getting a list of columns returned by a CFQUERY tag in ColdFusion
[6] Querying a query using the CFQUERY tag in ColdFusion
[7] Determining if a variable is a query object in ColdFusion
[8] Rounding numbers in ColdFusion
[9] Specifying the number of records shown in a query using the CFDUMP tag in ColdFusion
[10] Specifying a maximum number of results from a query using the CFQUERY tag in ColdFusion

Leave a Reply

Your email address will not be published.