Sequenzia
Sequenzia

Reputation: 31

Building XML from ColdFusion

I am building some XML within ColdFusion to send data to QuickBooks. I am able to build my variable with my data from a <cfoutput> fine. Like this:

<cfoutput query="get">

<cfset #x# =
'    
<InvoiceAddRq>
<InvoiceAdd>

    <CustomerRef>

        <ListID>XXXXX</ListID>          

    </CustomerRef>

    <ClassRef>

        <ListID>XXXXX</ListID>

    </ClassRef>

    <TxnDate>2010-11-04</TxnDate>


                <InvoiceLineAdd>

                    <ItemRef>
                        <ListID>XXXXX</ListID>
                    </ItemRef>

                    <Desc>XXXXX</Desc>
                    <Quantity>XXXXX</Quantity>
                    <Rate>XXXXX</Rate>          

                </InvoiceLineAdd>



</InvoiceAdd>

</InvoiceAddRq>
'
>

But I need to create XML where I loop through line item details with <cfloop> inside of the <cfset>. This is what I am trying to do:

<cfoutput query="get">

<cfset #x# =
'    
<InvoiceAddRq>
<InvoiceAdd>

    <CustomerRef>

        <ListID>XXXXX</ListID>          

    </CustomerRef>

    <ClassRef>

        <ListID>XXXXX</ListID>

    </ClassRef>

    <TxnDate>2010-11-04</TxnDate>

        <cfquery name="getDetails">

        </cfquery>

            <cfloop query="getDetails">

            <InvoiceLineAdd>

                <ItemRef>
                    <ListID>XXXXX</ListID>
                </ItemRef>

                <Desc>XXXXX</Desc>
                <Quantity>XXXXX</Quantity>
                <Rate>XXXXX</Rate>          

            </InvoiceLineAdd>

        </cfloop>           

</InvoiceAdd>

</InvoiceAddRq>
'
>

This is obviously not working right because it is seeing the and the as attributes of the XML. I am trying to figure out how to write some XML then do my query and loop to get the line item detail then go back into XML. I am stumped on how to do this. I hope this makes sense and any help would be greatly appreciated.

Upvotes: 3

Views: 1099

Answers (3)

Jason
Jason

Reputation: 276

Depending on the version of CF you are using you may want to 'roll your own' response and simply generate the XML string manually. If you want to output the XML to the screen, make sure to do a reset so you don't send anything but the raw XML to the screen. Also, ensure CF Debugging is turned off when viewing w/in the browser otherwise the output will not display properly.

<cfsilent>
<cfset retVal = "">
<cfquery name="get">
      DO SOMETHING HERE
</cfquery>
<cfloop query="get">
   <cfset retVal &= "<InvoiceAddRq><InvoiceAdd><CustomerRef><ListID>XXXXX</ListID></CustomerRef>">
   <cfset retVal &= "<ClassRef><ListID>XXXXX</ListID></ClassRef><TxnDate>2010-11-04</TxnDate>">
   <cfquery name="getDetails">
      DO SOMETHING HERE
   </cfquery>
   <cfloop query="getDetails">
      <cfset retVal &= "<InvoiceLineAdd><ItemRef><ListID>XXXXX</ListID></ItemRef>">
      <cfset retVal &= "<Desc>XXXXX</Desc><Quantity>XXXXX</Quantity><Rate>XXXXX</Rate></InvoiceLineAdd>">
   </cfloop>           
   <cfset retVal &= "</InvoiceAdd></InvoiceAddRq>">
</cfloop>
</cfsilent>
<cfcontent reset="yes">#retVal#</cfcontent>

Upvotes: 0

Ken Redler
Ken Redler

Reputation: 23943

You can try <cfxml> or <cfsavecontent> to build your XML string.

cfxml:

<cfxml variable="your_xml_var" caseSensitive="yes">
  <InvoiceAddRq>
    <Anothertag>
    </Anothertag>
    <cfloop query="your_query">
      <Somedata foo="#your_query.bar#">
      #your_query.blah#
      </Somedata>
    </cfloop>
  </InvoiceAddRq>
</cfxml>

cfsavecontent:

<cfsavecontent variable="your_xml_var">
  <InvoiceAddRq>
    <cfloop query="your_query">
      <Anothertag />
    </cfloop>
  </InvoiceAddRq>
</cfsavecontent>

Upvotes: 7

Ryan Guill
Ryan Guill

Reputation: 13896

Try wrapping your xml building in cfoutput and cfsavecontent If you are in a .cfm or .cfc file, it should not think that cfquery or cfloop (or any cf~ tag for that matter) is actually part of the xml, that will be stripped out while the page is being executed.

Upvotes: 1

Related Questions