cfxml vs cfsavecontent - 无法使用cfxml构建xml [英] cfxml vs cfsavecontent - unable to build xml using cfxml

查看:482
本文介绍了cfxml vs cfsavecontent - 无法使用cfxml构建xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个自定义XML文件,并将构建分成单独的函数,但我有麻烦这样做与cfxml。这个例子显然很简单。

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.

这很好用:

<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字符串:

But this produces an XML string with spaces:

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

如果我使用 cfxml 在BuildCarXML的cfreturn上的 XMLParse(),我得到以下错误:

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.

是否可以使用cfxml?

Is it possible to do this using cfxml?

推荐答案

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

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

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

因此无法嵌入您的 xmlCar 。因此,对于 AddMakeElement(),继续使用< cfsavecontent> ,或只是 return ; make> Something< / make>

Therefore it couldn't be embedded into your xmlCar. So for AddMakeElement(), keep using <cfsavecontent>, or just return "<make>Something</make>".

这篇关于cfxml vs cfsavecontent - 无法使用cfxml构建xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆