views:

157

answers:

5

I am building a site that is an interface used to create XML files that are read as input by a server side program.

The website allows users to dynamically create blocks of HTML. Each block can be thought of as an object and contains several input fields. When the user submits the form, the data is turned into an XML file.

What is the best way to preserve/rebuild the user generated HTML across the post request? I am using JQuery, but not AJAX.

A: 

Why are you processing the HTML into an XML if you want to preserve the HTML?

Just keep the HTML on the server, then when making an ajax request you can get the entire HTML tree and stick it in the DOM (without any re-building).

Luca Matteis
A: 

I would like to note modern XHTML aims to be a subset of XML. And I think Luca is right.

Aram Verstegen
A: 

Sorry, I was unclear. The XML file is for a server side program that takes XML as it's input.

Spencer
+2  A: 

You're probably looking for XML's CDATA.

<post>
<![CDATA[

<p>Hello, world!
<span style="color: green;">Green text</span>
<!-- oops, didn't close the p! -->

<ul>
    <li>list
    <li>doesn't
    <li>have closing
    <li>&lt;/li> (note the lack of use of &gt;)
</ul>

]]>
</post>

Just be sure to escape the ]]> in the user's input, else they may exploit your use of CDATA and mangle your XML!

strager
+1  A: 

What strager said. Plus, with Javascript you can get the HTML string for any element:

document.getElementById('myimportantthing').innerHTML

And send that to your server for inclusion in your CDATA XML element and you should be good.

Although the whole idea of capturing HTML to send to an API for some purpose just reeks of a code smell, without knowing what you are up to I can't say much more about that.

Squeegy