我使用ColdFusion从数据库条目创建XML文档,当创建XML时,<和>采用<和>格式。因此,在创建XML之前,有一种方法可以将<更改为<
下面是代码和输出示例:
<cfquery name="messages" datasource="showcase_Uk">
select * from t_items where pid = 2 and spid = 45
</cfquery>
<cfset myStruct = StructNew() />
<cfset mystruct.link = "http://showcase.com" />
<cfset myStruct.title = "Examples" />
<cfset mystruct.description = "Examples from UK Showcase" />
<cfset mystruct.pubDate = Now() />
<cfset mystruct.version = "rss_2.0" />
<cfset myStruct.item = ArrayNew(1) />
<cfloop query="messages">
<cfset myStruct.item[currentRow] = StructNew()>
<cfset myStruct.item[currentRow].guid = structNew()>
<cfset myStruct.item[currentRow].guid.isPermaLink="YES">
<cfset myStruct.item[currentRow].guid.value = xmlFormat(#messages.id#)>
<cfset myStruct.item[currentRow].pubDate = createDate(year(#messages.uploadDate#), month(#messages.uploadDate#), day(#messages.uploadDate#))>
<cfset myStruct.item[currentRow].title = xmlFormat(#messages.name#)>
<cfset myStruct.item[currentRow].description = StructNew() />
<cfset myStruct.item[currentRow].description.value = xmlFormat(#messages.description#)>
</cfloop>
<cffeed action="create" name="#myStruct#" overwrite="true" xmlVar="myXML">
<cffile action="write" file="e:\domains\showcase.com\wwwroot\ukshowcasefeed.xml" nameconflict="overwrite" output="#XMLFormat(myXML)#">
<cffile action="read" file="e:\domains\showcase.com\wwwroot\ukshowcasefeed.xml" variable="myfile">
<cfoutput>#myfile#</cfoutput>下面是它生成的代码的屏幕截图:

我试过ReplaceNoCase,但这一点也改变不了它。我相信这可能和Regex有关,但我真的不知道。
发布于 2015-08-05 09:03:24
简单回答:
不要在编写文件时使用XmlFormat(),这是导致您看到的双重编码的原因。
较长的答覆:
我建议您使用映射来创建提要:
<cfquery name="messages" datasource="showcase_Uk">
select
id, uploadDate, name, description, 'yes' as isPermaLink
from
t_items
where
pid = 2 and spid = 45
</cfquery>
<cfset feedMeta = {
version: "rss_2.0",
title: "Examples",
link: "http://showcase.com",
publisheddate: Now(),
description: "Examples from UK Showcase"
}>
<cfset feedMap = {
title: "name",
content: "description",
publisheddate: "uploadDate",
id: "id",
idpermalink: "isPermaLink"
}>
<cffeed
action="create"
properties="#feedMeta#" columnMap="#feedMap#" query="#messages#"
xmlvar="feedXml"
>
<cffile
action="write"
file="#ExpandPath('/ukshowcasefeed.xml')#" nameconflict="overwrite" charset="utf-8"
output="#feedXml#"
>注意创建提要的简单声明性方法。没有循环,没有手动结构创建,只有数据驱动的输入到输出的映射.
还要注意charset="utf-8"在<cffile>上,如果您不想遇到编码问题,这是必不可少的。
如果您修改了查询,使其具有正确的列名,那么甚至不需要feedMap结构:
<cfquery name="messages" datasource="showcase_Uk">
select
id,
'yes' as idpermalink
uploadDate as publisheddate,
name as title,
description as content
from
t_items
where
pid = 2 and spid = 45
</cfquery>
<cffeed
action="create"
properties="#feedMeta#" query="#messages#"
xmlvar="feedXml"
>https://stackoverflow.com/questions/31827866
复制相似问题