ColdFusion中如何正确生成多行XML条目?(避免CF标签嵌套)
Got it, let's fix this properly. Nesting <cfoutput> inside a <cfset> isn't valid ColdFusion syntax—cfset is for variable assignments, not embedding template tags directly. Here are a couple of clean, optimal approaches to generate your XML items and append them to soapBody:
Approach 1: Direct String Concatenation with <cfoutput> (Simple & Straightforward)
This is the most intuitive method for basic XML fragment building. We'll loop through your query using <cfoutput> and append each formatted <item> block to soapBody. Critical note: Always use XmlEncode() on your query values to escape special characters (like &, <, >) that would break XML validity.
<!--- Initialize soapBody if it hasn't been set yet ---> <cfset soapBody = "" /> <!--- Loop through your query and append each item's XML to soapBody ---> <cfoutput query="getitems"> <cfset soapBody &= ' <item id="#XmlEncode(id)#"> <unitPrice>#XmlEncode(getitems.unitprice)#</unitPrice> <quantity>#XmlEncode(getitems.quantity)#</quantity> <productname>#XmlEncode(getitems.productname)#</productname> <taxAmount>#XmlEncode(getitems.taxamount)#</taxAmount> <unitOfMeasure>#XmlEncode(getitems.unitofmeasure)#</unitOfMeasure> <taxRate>#XmlEncode(getitems.taxrate)#</taxRate> <totalAmount>#XmlEncode(getitems.totalamount)#</totalAmount> <grossNetIndicator>#XmlEncode(getitems.grossnetindicator)#</grossNetIndicator> </item>' /> </cfoutput>
Approach 2: Use ColdFusion XML Objects (More Robust for Complex SOAP Bodies)
If your soapBody is part of a larger, structured SOAP request, building XML via ColdFusion's native XML functions is safer. This method automatically handles XML syntax rules and avoids manual string concatenation errors.
<!--- Parse your existing soapBody into an XML object (skip if you're building it from scratch) ---> <cfset soapXml = XmlParse(soapBody) /> <!--- Target the parent node where <item> elements should be added (adjust the XPath to match your SOAP structure) ---> <cfset itemsParent = XmlSearch(soapXml, "//yourTargetParentNode")[1] /> <cfloop query="getitems"> <!--- Create a new <item> element ---> <cfset itemNode = XmlNew() /> <cfset itemNode.XmlName = "item" /> <!--- Add the "id" attribute ---> <cfset XmlAddAttribute(itemNode, "id", getitems.id) /> <!--- Add child elements with encoded values ---> <cfset XmlAddChild(itemNode, "unitPrice", XmlEncode(getitems.unitprice)) /> <cfset XmlAddChild(itemNode, "quantity", XmlEncode(getitems.quantity)) /> <cfset XmlAddChild(itemNode, "productname", XmlEncode(getitems.productname)) /> <cfset XmlAddChild(itemNode, "taxAmount", XmlEncode(getitems.taxamount)) /> <cfset XmlAddChild(itemNode, "unitOfMeasure", XmlEncode(getitems.unitofmeasure)) /> <cfset XmlAddChild(itemNode, "taxRate", XmlEncode(getitems.taxrate)) /> <cfset XmlAddChild(itemNode, "totalAmount", XmlEncode(getitems.totalamount)) /> <cfset XmlAddChild(itemNode, "grossNetIndicator", XmlEncode(getitems.grossnetindicator)) /> <!--- Append the new item to the parent node ---> <cfset XmlAppendChild(itemsParent, itemNode) /> </cfloop> <!--- Convert the XML object back to a string for your SOAP request ---> <cfset soapBody = XmlSerialize(soapXml) />
Which to Choose?
- Use Approach 1 for quick, simple XML fragment building where you're just appending items to a string.
- Use Approach 2 for full SOAP requests or complex XML structures—it’s more maintainable and less prone to syntax errors.
内容的提问来源于stack exchange,提问作者Unohoo

