Cfxml vs cfsavecontent - cannot create xml using cfxml

I am trying to create my own XML file and break it into separate functions, but I can not do it with cfxml. This example is obviously simplified.

This works great:

<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 creates an XML string with spaces:

<?xml version="1.0" encoding="UTF-8" ?> <car> <name>Ferrari</name> <make>Something</make> </car>

If I use cfxmlor even do XMLParse()cfreturn 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.

Can this be done using cfxml?

+1
source share
1 answer

In AddMakeElement(), if you use <cfxml>and toString(), the output is:

<?xml version="1.0" encoding="UTF-8"?> <make>Something</make>

Therefore, it cannot be embedded in yours xmlCar. Therefore, for AddMakeElement()use <cfsavecontent>or simply return "<make>Something</make>".

+2

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


All Articles