cfxml vs cfsavecontent - unable to build xml using cfxml

杀马特。学长 韩版系。学妹 提交于 2020-01-16 01:07:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!