Question

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?

Was it helpful?

Solution

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>".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top