cfxml vs cfsavecontent - 无法使用cfxml构建xml

时间:2014-02-07 02:11:33

标签: coldfusion coldfusion-9

我正在尝试构建一个自定义XML文件并将构建器拆分为单独的函数,但是我在使用cfxml时遇到了麻烦。这个例子显然是简化的。

这很好用:

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

但这会产生一个带空格的XML字符串:

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

如果我在BuildCarXML的cfreturn上使用cfxml,甚至做XMLParse(),我会收到以下错误:

An error occured while Parsing an XML document.

The processing instruction target matching "[xX][mM][lL]" is not allowed.

是否可以使用cfxml执行此操作?

1 个答案:

答案 0 :(得分:2)

AddMakeElement()中,如果您使用<cfxml>toString(),则输出为:

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

因此无法将其嵌入xmlCar。因此,对于AddMakeElement(),请继续使用<cfsavecontent>,或仅使用return "<make>Something</make>"

相关问题