|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Preserve HTML format when inserting into HTML
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:Jake Munson 09/03/10 04:14 P As far as I know, CF will not encode HTML unless you tell it to. AreRaymond Camden 09/05/10 11:53 A >As far as I know, CF will not encode HTML unless you tell it to. AreJake Munson 09/07/10 02:13 P >As far as I know, CF will not encode HTML unless you tell it to. AreJake Munson 09/07/10 02:13 P >As far as I know, CF will not encode HTML unless you tell it to. AreJake Munson 09/07/10 02:13 P When verifying the dumped text throughout the process, you're lookingdenstar 09/07/10 03:07 P > When verifying the dumped text throughout the process, you're lookingJake Munson 09/07/10 04:15 P When you cfdump something, it escapes it. So if you view source it,Raymond Camden 09/07/10 04:16 P >When you cfdump something, it escapes it. So if you view source it,Jake Munson 09/07/10 04:59 P So wait - you put the string into an XML object?Raymond Camden 09/07/10 05:06 P >So wait - you put the string into an XML object?Jake Munson 09/07/10 05:48 P > Before the above XML insert this HTML is not escaped (as explained inJake Munson 09/07/10 05:53 P Normally when you put HTML into an XML node, it has to be withinRaymond Camden 09/07/10 09:14 P Weird. I can't reproduce this:Raymond Camden 09/07/10 09:16 P > But like I said earlier, I created a workaround so I'm not stressingJake Munson 09/08/10 05:05 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 Btw - I have to say I was very confused initially because it soundedRaymond Camden 09/11/10 02:57 A Well, that is what I thought was happening originally, but as I went throughJacob Munson 09/11/10 03:20 A >Normally when you put HTML into an XML node, it has to be withinJake Munson 09/07/10 09:42 P 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? 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 ----- >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)#"> >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)#"> >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. 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 ----- > 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. 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 ----- >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. 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 ----- >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> > 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. 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 ----- 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 ----- 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 ----- > 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. 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 ----- I'm digging. This came up 3 years ago too: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32864 ----- Excess quoted text cut - see Original Post for more ----- 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 ----- >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!! 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 ----- 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 ----- >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. 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?
|
February 08, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||