Saving HTML to XML with Classic ASP

I am developing a very simple CMS system for my brother website.
I use classic ASP and the content is stored in an XML file, and I created a simple web form with a javascript rich text editor to edit the content. This means that the HTML code will be saved in the XML file. I am sure it will always be well-formed XHTML.

eg.

<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 a webpage, everything works fine, and while I am using <xsl:copy-of select="description/node()"/>the XSLT file, the HTML is displayed as it should.

The problem occurs when I try to save this HTML back to an XML file from a form. For this, I use the following code:

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 get the following in my XML file:

<description>&lt;strong&gt;18th&lt;/strong&gt; century &lt;span style="font-style: italic;"&gt;mahogany &lt;/span&gt;chest of drawers</description>

-, , HTML , .

- , .

Andy

+3
4

, XHTML, , DOM. .

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 Then.

+1

objField.text = "<! [CDATA [" Request.Form( "" ) "]] > "

.

+1

CDATA HTML

<![CDATA[ my_html_description ]]>

asp :

objField.text = "<![CDATA[" & Request.Form("description") & "]]>"
+1

Cordsen , .

, !

I found that I am getting more and more nested tags <description>in the final XML, so I first deleted the existing <description>node and then added a 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 work a bit so that I always have only one <description>node, but I have decided the worst bit.

Many thanks! Andy

+1
source

All Articles