It is a common programming requirement to persist
server-side XML in client-side HTML elements so that the XML is accessible to
client-side script. One method is to use the XML
Data Source object (DSO) in the client-side HTML to reference and store the
XML data. However, this method does not provide a cross-browser solution
because the XML DSO is an Internet Explorer-specific object.
Another
method is to use standard HTML in an <INPUT TYPE=HIDDEN> HTML element to
persist the XML. You can then use the
Value property of the hidden HTML element in client-side script to
access the XML that is persisted in it. Then, the client-side script can run
custom code to manipulate the XML (for example, load the XML into an instance
of an XML Document Object Model object on the client, and persist the XML to
disk).
Step-by-step example
If a newer version of MSXML has been installed in Side-by-Side
mode, then to run the sample code with that specific version, you must
explicitly use the GUIDs or ProgIDs for that version. For example, MSXML
version 4 only installs in side-by-side mode. Please refer to the following
article in the Microsoft Knowledge Base to see what code changes required to
run the sample code with the MSXML 4.0 parser:
305019�
MSXML 4.0 specific GUIDs and ProgIDs
For example, in the code below, you would create the
objects using:
Set clientSideXMLDoc = CreateObject("MSXML2.DomDocument.4.0")
and
Set serverSideXMLDoc = Server.CreateObject("MSXML2.DomDocument.4.0")
- In Notepad, create a new XML document named Books.xml, and
paste the following, well-formed XML:
<?xml version="1.0"?>
<Books>
<Book QTY="10">
<Title>Beginning XML</Title>
</Book>
<Book QTY="2">
<Title>Mastering XML</Title>
</Book>
</Books>
- Save Books.xml in one of your Microsoft Internet
Information Server (IIS) virtual directories.
- In Notepad, create a new ASP page named LoadHiddenXML.asp,
and paste the following code:
<HTML>
<HEAD>
<SCRIPT Language=VBScript>
Sub btnXML_onClick()
Dim clientSideXMLDoc
Set clientSideXMLDoc = CreateObject("Microsoft.XMLDOM")
clientSideXMLDoc.loadxml form1.txtXML.value
Msgbox clientSideXMLDoc.xml
End Sub
</SCRIPT>
</HEAD>
<BODY>
<%
Dim serverSideXMLDoc
Set serverSideXMLDoc = Server.CreateObject("Microsoft.XMLDOM")
serverSideXMLDoc.load Server.MapPath("books.xml")
%>
<FORM id="form1" method="POST" action="hiddenxml.asp">
<INPUT ID='txtXML' name='txtXML' TYPE='hidden' VALUE='<% =serverSideXMLDoc.xml %>'>
<INPUT TYPE=Button id="btnXML" value="Display Hidden XML">
</FORM>
</BODY>
</HTML>
- Save LoadHiddenXML.asp in the same IIS virtual directory
where you saved Books.xml.
- The ASP code contains server-side script to load the
contents of Books.xml into an instance of the Microsoft XML (MSXML) Document
Object Model (DOM). The loaded XML is then assigned as the value of the hidden
HTML element named txtXML.
Open Internet Explorer, and browse to LoadHiddenXML.asp.
When the page is displayed, click Display Hidden XML to load and display the XML (using a client-side MSXML DOM
object) that the server-side ASP script assigns to the txtXML hidden
element.