问题
I'm trying build a custom XML file and splitting the building into separate functions, but I'm having trouble doing this with cfxml. This example is obviously simplified.
This works fine:
<cfcomponent accessors="true">
<cfproperty name="Name" />
<cfproperty name="Model" />
<cfproperty name="Make" />
<cffunction name="BuildCarXML" output="false">
<cfsavecontent variable="xmlCar">
<cfoutput>
<?xml version="1.0" encoding="UTF-8" ?>
<car>
<name>#variables.Name#</name>
#AddMakeElement()#
</car>
</cfoutput>
</cfsavecontent>
<cfreturn xmlCar />
</cffunction>
<cffunction name="AddMakeElement">
<cfsavecontent variable="xmlMake">
<cfoutput>
<make>Something</make>
</cfoutput>
</cfsavecontent>
<cfreturn xmlMake />
</cffunction>
</cfcomponent>
But this produces an XML string with spaces:
<?xml version="1.0" encoding="UTF-8" ?> <car> <name>Ferrari</name> <make>Something</make> </car>
If I use cfxml, or even do an XMLParse() on the cfreturn of BuildCarXML, i get the following error:
An error occured while Parsing an XML document.
The processing instruction target matching "[xX][mM][lL]" is not allowed.
Is it possible to do this using cfxml?
回答1:
In AddMakeElement(), if you use <cfxml> and toString(), the output is:
<?xml version="1.0" encoding="UTF-8"?> <make>Something</make>
Therefore it couldn't be embedded into your xmlCar. So for AddMakeElement(), keep using <cfsavecontent>, or just return "<make>Something</make>".
来源:https://stackoverflow.com/questions/21617998/cfxml-vs-cfsavecontent-unable-to-build-xml-using-cfxml