Parsing XML in ColdFusion

In a previous post I showed how I sent XML out from ActionScript 3, here is how I parsed it in ColdFusion and created a PDF.  You have to check to see if there is any data in the XML or else it blows up.  Also,  notice the placement of the importing of the stylesheet to be applied to the PDF.

<!--- Check to see if there is any XML to parse --->
<cfif form.xmlPledgeData NEQ ''>
    <!--- Parse the XML --->
    <cfset myXml = XmlParse(#form.xmlPledgeData#)>
    <cfset elementRoot = myXml.XmlRoot>
    <cfset myArray=ArrayNew(1)>
    <cfset myArray=elementRoot>
    <cfset recordCounter = ArrayLen(elementRoot.XmlChildren) />
</cfif>
<!--- Create a PDF. By not putting a document value it opens directly in the browser. --->
<cfdocument format="pdf" overwrite="yes">
    <cfdocumentsection>
    <!--- Import your print stylesheet --->
    <link rel="stylesheet" href="print.css" type="text/css" media="print" />
    <body>
        <h1>Fun Run Pledge Form</h1>
        <table>
            <tr>
                <th width="25%">Name:</th>
                <td width="25%"><cfoutput>#form.userName#</cfoutput></td>
                <th width="25%">Today's Date:</th>
                <td width="25%"><cfoutput>#DateFormat(Now(),'MM/DD/YYYY')#</cfoutput></td>
            </tr>
            <tr>
                <th>Total Pledges:</th>
                <td><cfoutput>#form.totalQuantity#</cfoutput></td>
                <th> Total Amount Pledged:</th>
                <td><cfoutput>#LSCurrencyFormat(form.totalAmount)#</cfoutput></td>
            </tr>
        </table>
        <table>
            <tr>
                <th>Name</th>
                <th>Phone Number</th>
                <th>Amount</th>
            </tr>
            <!--- Check to see if there is any XML to loop over --->
            <cfif form.xmlPledgeData NEQ ''>
                <cfloop FROM="1" TO="#recordCounter#" INDEX="i">
                    <tr>
                        <td><cfoutput>#myXml.PledgeList.Pledge[i].name#</cfoutput></td>
                        <td><cfoutput>#myXml.PledgeList.Pledge[i].phone#</cfoutput></td>
                        <td><cfoutput>#myXml.PledgeList.Pledge[i].amount#</cfoutput></td>
                    </tr>
                </cfloop>
            </cfif>
        </table>
    </body>
    </cfdocumentsection>
</cfdocument>

Yes, I still use tables, but only for tabular data as that is what they were originally designed for.

One other thing I noticed is the parsed XML is cast as a String and ColdFusion doesn’t allow you to recast it to a number (Val()). If you need to do more math in ColdFusion then there is a way to query the XML data and recast it there, but I just decided to format the data as Currency and Phone Numbers in ActionScript3  and just display it in ColdFusion.

This entry was posted in CSS, ColdFusion, Flex, Web Design and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>