【问题标题】:Coldfusion XML ReplaceColdfusion XML 替换
【发布时间】:2018-04-26 21:21:17
【问题描述】:
不确定这是否合法。
我有一个使用 Coldfusion 读取的 xml 文档。
如果您有以下情况:
<tag1>
This is text <tag2>and this is highlighted text</tag2> which is in the middle of more text
</tag1>
我可以将所有 tag1 放入一个变量中,然后对变量进行替换,以将 tag2 更改为带有类的 html,从而使其突出显示吗?
或者有没有更好的方法?
【问题讨论】:
-
好吧,这不是有效的 XML。第二个<tag2> 应该是</tag2> 吗?如果是这样,请更新问题。您最好不要将其视为 XML,而只是进行字符串替换?或者看看 JSOUP (jsoup.org) 在这方面提供了什么?
标签:
xml
replace
coldfusion
【解决方案1】:
解决方案:
将 tag1 节点及其所有子标签变成一个字符串。
<cfset x = #tag1.xmlChildren[x]#>
将tag2替换为需要的html标签。
<cfset x = #Replace(x,"<tag2>","<mark>","ALL")#>
<cfset x = #Replace(x,"</tag2>","</mark>","ALL")#>
将字符串解析回xml。
<cfset x = XmlParse(#x#)>
把解析后的xml输出。
<cfoutput>#x.tag1.xmlText#</cfoutput>
【解决方案2】:
<tag1>
<![CDATA[
This is text <tag2>and this is highlighted text</tag2> which is in the middle of more text
]]>
</tag1>
这里会将其添加为字符串,而无需转义任何内容。它将被视为一个字符串,而不是 XML 结构的一部分。