如何在 ColdFusion 中设置 XML 文档的编码?

How can I set the encoding of an XML document in ColdFusion?

我正在使用 <cfxml>:

创建和 XML 对象
<cfxml variable="LOCAL.Xml">
    <Root>
        <Header>
            <CompanyName>ACME</CompanyName>
            <AccountNumber>ACM1234</AccountNumber>
            <FileTimeStamp>2020-05-19{T}22:13:51</FileTimeStamp>
            <TimeZone>EDT</TimeZone>
        </Header>
    </Root>
</cfxml>

当我将其转换为字符串时,XML 声明与 encoding="UTF-8" 一起添加到顶部。如何使用 CF11 指定不同的编码?

我试过了<cfprocessingdirective encoding="iso-8859-1">

我试过了<cfheader name="content-disposition" charset="iso-8859-1" value="attachment; filename=file.xml" />

我试过使用编码 "iso-8859-1" 将其转换为二进制,然后再转换回字符串。

每次我得到相同的结果。

这是从此处发布的 cfxml 的文档复制而来的 - CFXML

To convert an XML document object back into a string, use the ToString function, at which time ColdFusion automatically prepends the <?xml version="1.0" encoding="UTF-8" ?> XML declaration.

To change the declaration to specify another encoding, use the Replace function. To specify the encoding of the text that is returned to a browser or other application, use the cfcontent tag. The following example illustrates this process:

<cfprocessingdirective suppressWhiteSpace = "yes">
<cfcontent type="text/xml; charset=utf-16">
<cfxml variable="xmlobject">
    <breakfast_menu>
    <food>
        <name quantity="50">Belgian Waffles</name>
        <description>Our famous Belgian Waffles</description>
    </food>
    </breakfast_menu>
</cfxml>

<!--- <cfdump var="#xmlobject#">--->

<cfset myvar=toString(xmlobject)>
<cfset mynewvar=replace(myvar, "UTF-8", "utf-16")>

<!---<cfdump var="#mynewvar#">--->

<cfoutput>#mynewvar#</cfoutput>
</cfprocessingdirective>

The cfprocessingdirective tag prevents ColdFusion from putting white space characters in front of the XML declaration.

请注意“ColdFusion 自动预先添加 <?xml version="1.0" encoding="UTF-8" ?> XML 声明

他们推荐的方法是使用“Replace 函数”来改变它。所以对你来说,替换将变成:

<cfset myvar=toString(xmlobject)>
<cfset mynewvar=replace(myvar, "UTF-8", "iso-8859-1")>

您还想将 cfcontent 标签更改为:

<cfcontent type="text/xml; charset=iso-8859-1">