首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用< in ColdFusion替换&lt;

如何用< in ColdFusion替换&lt;
EN

Stack Overflow用户
提问于 2015-08-05 08:57:52
回答 1查看 299关注 0票数 0

我使用ColdFusion从数据库条目创建XML文档,当创建XML时,<>采用&lt;&gt;格式。因此,在创建XML之前,有一种方法可以将&lt;更改为<

下面是代码和输出示例:

代码语言:javascript
复制
<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有关,但我真的不知道。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-05 09:03:24

简单回答:

不要在编写文件时使用XmlFormat(),这是导致您看到的双重编码的原因。

较长的答覆:

我建议您使用映射来创建提要:

代码语言:javascript
复制
<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结构:

代码语言:javascript
复制
<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"
>
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31827866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档