如何替换<与<在ColdFusion中

时间:2015-08-05 08:57:52

标签: regex xml coldfusion rss

我正在使用ColdFusion从数据库条目创建XML文档,并且在创建XML时,<>的格式为&lt;&gt;。那么在创建XML之前,有没有办法将&lt;更改为<

下面是代码和输出示例:

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

然后是它生成的代码的屏幕截图:

screen shot of output

我已经尝试了ReplaceNoCase并且根本没有改变它。我相信这可能与Regex有关,但我真的不知道。

1 个答案:

答案 0 :(得分:7)

简单回答:

请勿在撰写文件时XmlFormat(),这是导致您看到双重编码的原因。

更长的答案:

我建议您使用映射来创建Feed:

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

请注意Feed创建的简单声明方法。没有循环,没有手动结构创建,只有数据驱动的输入到输出的映射。

另请注意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"
>