简体   繁体   中英

Coldfusion XML Replace

Not sure if this is a legitimate operation.

I have an xml document that I am using Coldfusion to read.

If you have the following:

 <tag1>
   This is text <tag2>and this is highlighted text</tag2> which is in the middle of more text
 </tag1>

Can I put all of tag1 into a variable and do a replace on the variable to change the tag2 into html with a class so that it comes out highlighted?

Or is there a better way?

Solution:

Turn the tag1 node and all of its children tags into a string.

 <cfset x = #tag1.xmlChildren[x]#>

Replace the tag2 with the need html tag.

 <cfset x = #Replace(x,"<tag2>","&lt;mark&gt;","ALL")#>
 <cfset x = #Replace(x,"</tag2>","&lt;/mark&gt;","ALL")#>

Parse string back to xml.

 <cfset x = XmlParse(#x#)>

Out put the parsed xml.

 <cfoutput>#x.tag1.xmlText#</cfoutput>
<tag1>
    <![CDATA[
        This is text <tag2>and this is highlighted text</tag2> which is in the middle of more text
    ]]>
</tag1>

This here will add it as a string without the need to escape anything. It will be treated as a string and not part of the XML structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM