House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Preserve HTML format when inserting into HTML

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Weird. I can't reproduce this:
Raymond Camden
09/07/10 09:16 P
Ray,
Jake Munson
09/08/10 06:01 P
I'm digging. This came up 3 years ago too:
Raymond Camden
09/08/10 09:38 P
Woot! Found it:
Raymond Camden
09/08/10 09:40 P
>Woot! Found it:
Jake Munson
09/11/10 01:03 A
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/03/2010 04:14 PM

I have an XML object that was populated from an XML file.  This XML comes from an external system that I do not control.  Here is the bit of XML that I'm struggling with: <parks>   <park>     <SiteAbstract>       <![CDATA[<font color="#045172"><b>Loveridge Bridge North</b></font>]]>     </SiteAbstract>   </park> </parks> I know this is valid XML...at least it works in the system that uses it. I am trying to write an updated value to this SiteAbstract tag, and the result will show up in this external system.  As you can see, the HTML inside the tag is NOT htmlencodedformat, it's just plain HTML.  Since SiteAbstract is using the CDATA tag, this is fine. The trouble is that when I try to insert HTML into this tag, ColdFusion is converting the HTML to entity tags.  Normally this would be fine, as people usually don't want plain HTML in XML.  But with this external system I need to preserve the HTML as is.  I have googled and dug in the docs for a while now and I can't find a way to force ColdFusion to leave the HTML as is. The only solution I have come up with (and this is NOT what I'd prefer to do) is to do <cfloop file="myXMLfile.xml", search for the XML tag I need to update (this part will be painful) and then manually change the XML tag contents and save the XML to disk. Any better ideas?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/05/2010 11:53 AM

As far as I know, CF will not encode HTML unless you tell it to. Are you 100% sure you aren't using htmlEditFormat on the data? ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 02:13 PM

>As far as I know, CF will not encode HTML unless you tell it to. Are >you 100% sure you aren't using htmlEditFormat on the data? I have traced the XML value from start to end, and it appears that the place where the HTML is being converted is when I write the XML to disk.  I am dumping the XML to the page all throughout the process, and it looks correct all the way until I write it to the file.  Before the file write the XML has the plain HTML, but once written to the file the HTML is converted to entity characters.  Here's what I'm doing: <cffile action="write" file="D:\parksMapFiles\parks.xml" output="#toString(parksXML)#">

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 02:13 PM

>As far as I know, CF will not encode HTML unless you tell it to. Are >you 100% sure you aren't using htmlEditFormat on the data? I have traced the XML value from start to end, and it appears that the place where the HTML is being converted is when I write the XML to disk.  I am dumping the XML to the page all throughout the process, and it looks correct all the way until I write it to the file.  Before the file write the XML has the plain HTML, but once written to the file the HTML is converted to entity characters.  Here's what I'm doing: <cffile action="write" file="D:\parksMapFiles\parks.xml" output="#toString(parksXML)#">

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 02:13 PM

>As far as I know, CF will not encode HTML unless you tell it to. Are >you 100% sure you aren't using htmlEditFormat on the data? I have traced the XML value from start to end, and it appears that the place where the HTML is being converted is when I write the XML to disk.  I am dumping the XML to the page all throughout the process, and it looks correct all the way until I write it to the file.  Before the file write the XML has the plain HTML, but once written to the file the HTML is converted to entity characters.  To write the XML to file I am using cffile and then for the output I use toString() on the xml object.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
denstar
09/07/2010 03:07 PM

When verifying the dumped text throughout the process, you're looking at the "view source" version, and not the one that's rendered by the browser, right? :Den -- The soul which has no fixed purpose in life is lost; to be everywhere, is to be nowhere. Michel de Montaigne ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 04:15 PM

> When verifying the dumped text throughout the process, you're looking > at the "view source" version, and not the one that's rendered by the > browser, right? Sorry for the multiple replies earlier...I have no idea why that happened. Yes, that's correct I'm using view source.  In fact, I even wrote the value to a file BEFORE inserting to the XML Object, and it looks fine in the file as well.   It's only when I insert to the XML object that the HTML is converted.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 04:16 PM

When you cfdump something, it escapes it. So if you view source it, you are seeing cfdump's escape. In other words, it isn't the REAL value. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 04:59 PM

>When you cfdump something, it escapes it. So if you view source it, >you are seeing cfdump's escape. In other words, it isn't the REAL >value. I suppose you probably saw my other response (I think we passed each other at the intersection), but if not... If I display the pre-xml value using CFOutput, the HTML looks fine.  If I write the pre-xml value to a file, the HTML looks fine.  If I insert the value to my XML object, the HTML gets escaped.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 05:06 PM

So wait - you put the string into an XML object? Maybe you can pastebin more of the code. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 05:48 PM

>So wait - you put the string into an XML object? > >Maybe you can pastebin more of the code. I have developed a workaround that avoids this problem, but for educational purposes here's the bit of code that seems to be hosing the HTML: <cfset parksXML.Parks.Park[i].SiteAbstract.XmlText = editCopy> "editCopy" is made up of various data, but this piece we are concerned with: <font color="#045172" size="12"><b>Trenner Park</b></font><br><br> Before the above XML insert this HTML is not escaped (as explained in a previous message).  After the insert it looks like this: <font color="#045172" size="12"><b>Trenner Park</b></font><br><br>

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 05:53 PM

> Before the above XML insert this HTML is not escaped (as explained in > a previous message).  After the insert it looks like this: > <font color="#045172" size="12"><b>Trenner > Park</b></font><br><br> I should reiterate that I am not dumping the XML object and seeing this escaped HTML...I am writing the XML to a file and then I see the escaped HTML.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 09:14 PM

Normally when you put HTML into an XML node, it has to be within cdata. So maybe CF is "fixing" the HTML for you? ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 09:16 PM

Weird. I can't reproduce this: <cfset s = "<font color=""red""><b>foo</b></font>"> <cfxml variable="x"> <root> <child name="one" /> </root> </cfxml> <cfset x.root.child[1].xmlText = s> <cfoutput> #x.root.child[1].xmltext# <p> #s# </cfoutput> The value is NOT escaped in the XML, nor in the original variable. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 09:44 PM

I did something similar in a test page, but using the XML functions instead of cfxml.  I was not able to reproduce it either...there's something odd about the original XML that is causing this, I guess.  But like I said earlier, I created a workaround so I'm not stressing about it.  It's just odd. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/08/2010 05:05 PM

> But like I said earlier, I created a workaround so I'm not stressing > about it.  It's just odd. I have found the same problem with a different XML file.  I am going to try to put together a proof of concept to see if other people besides me can recreate this issue.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/08/2010 06:01 PM

Ray, In addition to my previous email with proof of concept code, I am able to reproduce my problem using your code below by adding this cffile tag to the end of your code: <cffile action="write" file="#expandPath('foo.xml')#" output="#toString(x)#"> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/08/2010 09:40 PM

Woot! Found it: http://www.mail-archive.com/cf-talk@houseoffusion.com/msg312892.html I have _never_ seen this in use, but it works perfectly: <cfset s = "<font color=""red""><b>foo</b></font>"> <cfxml variable="x"> <root> <child name="one" /> </root> </cfxml> <cfdump var="#x#"> <cfset x.root.child[1].xmlCData =   s> <cfoutput> #x.root.child[1].xmltext# <p> #s# </cfoutput> <cffile action="write" file="#expandPath('foo.xml')#" output="#toString(x)#"> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/11/2010 01:03 AM

>Woot! Found it: > >http://www.mail-archive.com/cf-talk@houseoffusion.com/msg312892.html > >I have _never_ seen this in use, but it works perfectly: > ><cfset x.root.child[1].xmlCData =   s> Awesome!  xmlCData?  Who knew?  And it takes care of the cdata tag for you as well.  Very nice.  Thanks for finding this!!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/11/2010 02:57 AM

Btw - I have to say I was very confused initially because it sounded like you were saying the _original_ value was being escaped. Ie, you put S into X (x being an xml object, S being a string w/ html) and S changed, not X. Anyway - going to blog this up around noon. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jacob Munson
09/11/2010 03:20 AM

Well, that is what I thought was happening originally, but as I went through all the different tests I finally figured out that the ecaping occurred when you write the xml to a file. Sent with my Android phone On Sep 11, 2010 12:57 AM, "Raymond Camden" <rcamden@gmail.com> wrote: ----- Excess quoted text cut - see Original Post for more ----- output="#toString(x)#"> ----- Excess quoted text cut - see Original Post for more ----- to reproduce my problem using your code below by adding this cffile tag to the end of your code: >>>> <cffile action="write" file="#expandPath('foo.xml')#" output="#toString(x)#"> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/07/2010 09:42 PM

>Normally when you put HTML into an XML node, it has to be within >cdata. So maybe CF is "fixing" the HTML for you? Yeah, I was just simplifying things, I do have a cdata tag wrapping the HTML.   Doesn't help.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jake Munson
09/08/2010 05:48 PM

Ok, guys, I have a working proof of concept.  At least on my server, this simple 4 line code example illustrates the problem.  To recreate, dowload this file: http://techfeed.net/xmlHTMLTest.zip And then follow these steps. 1.  Extract the zip to your ColdFusion sites directory. 2.  BEFORE RUNNING THE .CFM, open up parkRegions.xml and note how the first <region> has normal (not escaped) HTML tags. 3.  Run xmlHTMLTest.cfm (no output is generated, so you'll just see a blank page). 4.  Open parkRegions.xml again and note how the first <region> now contains escaped HTML.  At least, for me on my servers it does (I tried this code on two different servers). Can you see what I'm doing wrong?


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 08, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29