I am developing a very simple CMS system for my brother's web site.
I am using Classic ASP and the content is stored in an XML file and I have created a simple web form with a javascript rich text editor for him to edit the content. This means that there will be HTML code being saved in the XML file. I am confident that this will always be well formed XHTML.e.g.
<content>
<item id="20110611103415" sort="1" status="P">
<description><strong>18th</strong> century <span style="font-style: italic;">mahogany </span>chest of drawers</description>
</item>
</content>
When displaying this on the web page it all works OK and as long as I use <xsl:copy-of select="description/node()"/>
in the XSLT file, the HTML displays as it should.
The problem comes when I try to save this HTML back into the XML file from the form. I am using the following code to do this:
set objXML = Server.CreateObject("MSXML2.DOMDocument")
objXML.async = false
strXMLFile = server.MapPath("content.xml")
objXML.load strXMLFile
Set objRoot = objXML.documentElement
Set objItem = objRoot.SelectSingleNode("item[@id='" & strID & "']")
Set objField = objSaleItem.SelectSingleNode("description")
objField.text = Request.Form("description")
objXML.save strXMLFile
When I do this I end up with the following in my XML file:
<description><strong>18th</strong> century <span style="font-style:开发者_如何学JAVA italic;">mahogany </span>chest of drawers</description>
I have scoured the web trying to find out how I can prevent the HTML being encoded like this but I can't find a solution anywhere.
If anyone can help me I'd be very grateful.
Thanks Andy
Even if you are confident that the results of your form are always valid XHTML you should validate it by loading it into the DOM parser. This will force it to be valid and allow you to save and get it out again.
set formDescriptionXML = Server.CreateObject("MSXML2.DOMDocument")
' loadXML returns a false if the XML is not valid
If Not (formDescriptionXML.loadXML (Request.Form("description"))) Then
' handle the load error
End If
Set objRoot = objXML.documentElement
Set objItem = objRoot.SelectSingleNode("item[@id='" & strID & "']")
Set objField = objSaleItem.SelectSingleNode("description")
objField.appendChild(formDescriptionXML.documentElement)
objXML.save strXMLFile
If the form can contain only text as well you can handle that condition with the If Then
block.
I would suggest to use the CDATA to encapsulate the HTML
<![CDATA[ my_html_description ]]>
so the asp code will look like this:
objField.text = "<![CDATA[" & Request.Form("description") & "]]>"
objField.text = "<![CDATA[" & Request.Form("description") & "]]>"
It will not get encoded.
Thanks to Cordsen for pointing me in the right direction here.
I used your suggested solution with a slight amendment and it is working OK so far!
I found that I was getting more and more nested <description>
tags in the final XML so I deleted the existing <description>
node first and then appended the new one:
frmDescription = Request.Form("description")
'Line breaks in the form are coming across as <br> instead of <br />'
frmDescription = replace(frmDescription, "<br>", "<br />")
Set formDescriptionXML = Server.CreateObject("MSXML2.DOMDocument")
formDescriptionXML.loadXML frmDescription
If Not formDescriptionXML Is Nothing Then
'Remove the existing description node'
Set objRemove = objSaleItem.SelectSingleNode("description")
objSaleItem.removeChild objRemove
'Create the new description node '
objSaleItem.appendChild formDescriptionXML.documentElement
End If
I still need to do a bit of work to nake sure I always only have one <description>
node but I have solved the worst bit.
Thanks very much! Andy
精彩评论