Creating XML from ColdFusion

I am creating XML in ColdFusion to send data to QuickBooks. I can build my variable with my data from <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 view position data using <cfloop>internally <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 obviously does not work correctly, because it sees the attributes and as XML. I am trying to figure out how to write some XML, then execute my query and loop to get the position detail, and then return to XML. I don’t understand how to do this. I hope this makes sense and any help would be greatly appreciated.

+3
source share
3 answers

<cfxml> <cfsavecontent>, XML-.

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>
+7

xml cfoutput cfsavecontent. .cfm .cfc, , cfquery cfloop ( cf ~ , ) xml, .

+1

CF, , "" XML . XML , reset, , XML . , , CF , .

<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>
0

Source: https://habr.com/ru/post/1773675/


All Articles