Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building XML from ColdFusion

Tags:

coldfusion

xml

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.

like image 669
Sequenzia Avatar asked Sep 09 '26 16:09

Sequenzia


2 Answers

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>
like image 175
Ken Redler Avatar answered Sep 12 '26 13:09

Ken Redler


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.

like image 31
Ryan Guill Avatar answered Sep 12 '26 15:09

Ryan Guill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!