I'm creating a plugin/bookmarklet that will take an XML document embedded within the <textarea>
of a web page, make some changes to that XML, and then place the modified XML back in the <textarea>
(to get submitted back to the server). My code works perfectly except when it comes to the CDATA sections of the XML. When my jQuery code converts the XML into HTML, it converts all the CDATA sections into comments. Thus
<![CDATA[${Some Value}]]>
becomes
<!--[CDATA[${Some Value}]]-->
Sometimes some of the data within the CDATA sections also gets messed up. I read in some posts that this same thing happens when using AJAX calls to retrieve XML data. The solution was to include the correct header type: 'Content-Type: text/xml'
However, I don't know how to use that information to solve my problem - since I'm only working with XML already on the page.
For further background, the way I'm getting the XML out of the <textarea>
on the page so that I can further manipulate it is with the following:
var myXml = $('textarea#myID').val();
myXml = $('<div id="myDiv" />').html(myXml);
and I place the modified XML back in the with the following:
$('textarea#myID').val($('#myDiv').html());
Does anyone know how I might be able to get around the issue with how the CDATA sections get turned into <!--comments-->
when the XML first gets converted into HTML?